flutter入门到放弃3

页面跳转

MaterialApp 指定Map路线名称,与Android中注册AndroidManifest.xml类似

void main() {
  runApp(MaterialApp(
    home: const MyAppHome(), // Becomes the route named '/'.
    routes: <String, WidgetBuilder>{
      '/a': (context) => const MyPage(title: 'page A'),
      '/b': (context) => const MyPage(title: 'page B'),
      '/c': (context) => const MyPage(title: 'page C'),
    },
  ));
}
  • 使用Navigator跳转
    Navigator.of(context).pushNamed(‘/b’);
  • 关闭页面
    Navigator.pop(context);
  • 关闭多个页面
    Navigator.popUntil(context,ModalRoute.withName(‘xxx'));
  • 关闭页面并打开新页面
    Navigator.popAndPushNamed(context,’/text’)

导入第三方包

在项目中找到pubspec.yaml文件,里面包含

  • Dependencies 导入第三方包 例如 http: 0.13.4
  • dev_dependencies导入的是测试服
    保存后续运行
    flutter pub get
    才会去拉取
  • 使用脚本添加
    flutter pub add http

异步操作

  • Async / await使用与kotlin协程类似
Future<void> loadData() async {

  var dataURL = Uri.parse('[https://jsonplaceholder.typicode.com/posts');](https://jsonplaceholder.typicode.com/posts');)

  http.Response response = await http.get(dataURL);

  setState(() {

    widgets = jsonDecode(response.body);

  });

}
  • Isolate.spawn 功能类似开启一个新的线程
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容