flutter一个构建函数返回null

时间:2020-08-21 16:23:05

标签: flutter dart

我正在尝试使用FutureBuilder,但其显示A构建函数的错误返回null

我的代码

class _EventsState extends State<Events> {
  @override

  Future<List> doSomeAsyncStuff() async {
    final storage = new FlutterSecureStorage();
    String value = await storage.read(key: 'token');
    print(value);

    String url = 'http://sublimeapi.netcodesolution.com/api/NewsAndEvents/';

    String token = value;
    final response = await http.get(url, headers: {
      'Content-Type': 'application/json',
      'Accept': 'application/json',
      'Authorization': 'Bearer $token',
    });
    print('Token : ${token}');
    var eventData = json.decode(response.body);
    print(eventData["Data"]);
    List _events = eventData["Data"];
    return _events;
  }

  @override
  Widget build(BuildContext context) {
    double statusBarHeight = MediaQuery
        .of(context)
        .padding
        .top;
    return Expanded(
      child: FutureBuilder(
          future: doSomeAsyncStuff(),
          builder: (BuildContext context, AsyncSnapshot<List> snapshot) {
            List<Widget> children;
            if (snapshot.connectionState == ConnectionState.done) {
              if (snapshot.hasData) {
                print('working');
                print(snapshot.data);
                return Container(
                  child:  Column(
                      children: <Widget>[
                        Text('working')
                      ],
                    ),
                );
              }
            }
          }),
    );
  }

正如您在代码中看到的那样,我正在从API提取数据并正常工作。在代码中,我在setState中打印_events的值,它也像这样打印值

I/flutter (32627): [{Id: 38, Description: Two days Workshop on Prevention of Suicide organized for GPs of Thar., ImagePath: /journals/2e8a55f3-6612-4b23-a0ea-e91022c159a8.pdf, CreatedBy: 4447, CreatedOn: 2019-09-18T14:56:13.357, Active: false, Type: Events, Attachment: null, AttachmentType: Events}

我需要在以后的小部件中打印此数据的Description值,但不知道为什么它显示错误

1 个答案:

答案 0 :(得分:1)

错误清楚地表明了!它返回空值。
因此,您必须退货!做这样的事情,

 Future<List> doSomeAsyncStuff() async {
    final storage = new FlutterSecureStorage();
    String value = await storage.read(key: 'token');
    print(value);

    String url = 'http://sublimeapi.netcodesolution.com/api/NewsAndEvents/';

    String token = value;
    final response = await http.get(url, headers: {
      'Content-Type': 'application/json',
      'Accept': 'application/json',
      'Authorization': 'Bearer $token',
    });
    print('Token : ${token}');
    var eventData = json.decode(response.body);
    print(eventData["Data"]); 
    List _events = eventData["Data"];
    return _events; 
  }

而且,我们在这里错过了另一个案例。

 Scaffold(
   appbar: AppBar(
      title: const Text('Sample Future Builder'),
    ),
   body: Expanded(
      child: FutureBuilder(
          future: doSomeAsyncStuff(),
          builder: (BuildContext context, AsyncSnapshot<List> snapshot) {
             
            if (snapshot.connectionState == ConnectionState.done) {
              if (snapshot.hasData) {
                print('working');
                print(snapshot.data);
                return Container(
                  child:  Column(
                      children: <Widget>[
                        Text('working')
                      ],
                    ),
                );
              }
            }
            return Center(child: Text("Not Loaded Yet!!"),)
          }
       ),
    ),
  );

希望能解决您的问题!

提示:将所有小部件移到Scaffold下。这将是最佳做法。 Refer this