Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

await 报错了,请更新下教程示例 #25

Closed
mogocat opened this issue Jul 7, 2018 · 5 comments
Closed

await 报错了,请更新下教程示例 #25

mogocat opened this issue Jul 7, 2018 · 5 comments

Comments

@mogocat
Copy link

mogocat commented Jul 7, 2018

error: Undefined name 'await' in function body not marked with 'async'. (undefined_identifier_await at [android] lib/main.dart:9)

@mogocat
Copy link
Author

mogocat commented Jul 7, 2018

我是dart新手,flutter内置了dart2,await和async貌似要被换成then的语法,采用Future类型实现具体参照这篇帖子

Dio dio = new Dio();
Response response=await dio.get("https://www.google.com/");
print(response.data);

需要修改为
need to be changed to

 Dio dio = new Dio();
 dio.get("https://www.baidu.com").then((response) {print(response.data);});

目前运行良好
It works for now

@wendux
Copy link
Contributor

wendux commented Jul 8, 2018

@mogocat 不是的,你需要把代码放到async函数中才能使用await,示例代码是片段,你可以查看工程example下的完整示例。

@mogocat
Copy link
Author

mogocat commented Jul 9, 2018

@wendux 感谢回复,我用then调用了,能正常使用。

@mogocat mogocat closed this as completed Jul 9, 2018
@mogocat
Copy link
Author

mogocat commented Jul 9, 2018

抱歉我只是按照教程敲Widget上手,对这些不太熟悉。如果其他人也按照文档教程写Widget上手的话,建议还是用then,如果熟悉async和await用法,再参照example。

@mogocat
Copy link
Author

mogocat commented Jul 9, 2018

添加了一个示例

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

class MyApp extends StatelessWidget {
  MyApp (){
    var dio = new Dio();
    dio.get("https://www.baidu.com").then((response) {
      print(response.data);
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Welcome to Flutter',
      home: Scaffold(
        appBar: AppBar(
          title: Text('Welcome to Flutter'),
        ),
        body: Center(
          child: Text('Hello World'),
        ),
      ),
    );
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants