flutter desktop学习笔记

1 工具准备

我用的是Windows平台,需要Visual Studio 2017 or 2019,并且包含“C++桌面开发负载”。其他平台可以去Github主页查看。

flutter desktop学习笔记_第1张图片
或者ubuntu平台需要安装git和clang

sudo apt install git
sudo apt install clang

2flutter切换到master版本

在终端中输入

flutter channel master

3设置运行环境

vscode (windows)输入$env:ENABLE_FLUTTER_DESKTOP=“true”
或者
cmd(windows) 输入set ENABLE_FLUTTER_DESKTOP=true
或者
vscode (ubuntu)export ENABLE_FLUTTER_DESKTOP=true

4运行

flutter run

PS D:\flutter\flutter-desktop-embedding\example> $env:ENABLE_FLUTTER_DESKTOP=“true”
PS D:\flutter\flutter-desktop-embedding\example> flutter run
Flutter assets will be downloaded from https://storage.flutter-io.cn. Make sure you trust this source!
Downloading windows-x64 tools… 2.9s
Downloading windows-x64 tools… 0.3s
Running “flutter pub get” in example… 0.8s
Launching lib\main.dart on Windows in debug mode…
Building Windows application…

出现错误

我在装完visual studio 2019 社区版以后,运行flutter run显示

Unable to find suitable Visual Studio toolchain. Please run flutter doctor for more details.
flutter desktop学习笔记_第2张图片

关于异步get请求(async)

pubspec.yaml
dependencies:
dio: any
json_annotation: ^0.2.3
dev_dependencies:
build_runner: ^0.9.0
json_serializable: ^0.5.4

新建一个用于连接的类

import 'dart:convert';
import 'package:dio/dio.dart';
void getTest() async{
  Response response;
  var dio = new Dio();
  response = await dio.get("https://www.test.com/public/newhome/get/10007");
  
if (response != null) {
  var res2Json  = json.encode(response.data);
  Map map=json.decode(res2Json);
  print("你好"+map["msg"]);
}
  
}

在网上找了一个执行请求的实例

import 'package:flutter/material.dart';
import 'dart:convert';
import 'dart:io';

void main() => runApp(MyApp());

var hotMovies =
    'https://api.douban.com/v2/movie/in_theaters?apikey=0df993c66c0c636e29ecbb5344252a4a';

class MyApp extends StatelessWidget {

  var movies = '';

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'HttpClient请求示例',
        theme: new ThemeData(
          primaryColor: Colors.white,
        ),
        home: new Scaffold(
          appBar: new AppBar(
            title: new Text('HttpClient请求示例'),
          ),
          body: new Column(children: [
            new RaisedButton(
                child: new Text('获取电影列表'), onPressed: getFilmList),
            new Expanded(
              child: new Text('$movies'),
            )
          ]),
        ));
  }

 void getFilmList() async {
    try {
      HttpClient httpClient = new HttpClient();
      HttpClientRequest request = await httpClient.getUrl(Uri.parse(hotMovies));
      HttpClientResponse response = await request.close();
      var result = await response.transform(utf8.decoder).join();
      movies = result;
      print('movies'+result);
      httpClient.close();
    }catch(e){
      print('请求失败:$e');
    }
  }
}

本文未经本人同意,可以随意转载。如果非要通知本人,可以来米聊flutter群,群号:50338469

你可能感兴趣的:(flutter)