【问题标题】:info: This function has a return type of 'Widget', but doesn't end with a return statementinfo:此函数的返回类型为“Widget”,但不以 return 语句结尾
【发布时间】:2020-01-17 09:27:08
【问题描述】:

我正在尝试弄清楚为什么当我的 Streambuilder 返回时出现此错误

import 'package:flutter/material.dart';
    import 'package:http/http.dart' as http;
    import 'dart:async';
    import 'dart:convert';
    import 'package:google_fonts/google_fonts.dart';

    import 'package:paylaterapp/screens/MerchantDetails.dart';

    class MerchantList extends StatefulWidget {
      @override
      _MerchantListState createState() => new _MerchantListState();
    }

    class _MerchantListState extends State<MerchantList> {
      StreamController _postsController;
      final GlobalKey<ScaffoldState> scaffoldKey = new GlobalKey<ScaffoldState>();

      int count = 1;

      Future fetchPost([howMany = 10]) async {
        final response = await http.get('http://localhost/categories/fitness/$howMany');

        if (response.statusCode == 200) {
          return json.decode(response.body);
        } else {
          throw Exception('Failed to load merchant');
        }
      }

      loadPosts() async {
        fetchPost().then((res) async {
          _postsController.add(res);
          return res;
        });
      }

      showSnack() {
        return scaffoldKey.currentState.showSnackBar(
          SnackBar(
            content: Text('New content loaded'),
          ),
        );
      }

      Future<Null> _handleRefresh() async {
        count++;
        print(count);
        fetchPost(count).then((res) async {
          _postsController.add(res);
          showSnack();
          return null;
        });
      }

      @override
      void initState() {
        _postsController = new StreamController();
        loadPosts();
        super.initState();
      }

      @override
      Widget build(BuildContext context) {
        return new Scaffold(
          key: scaffoldKey,
          appBar: new AppBar(
            title: Text('Explore Retailers',
                style: GoogleFonts.rubik(
                    fontWeight: FontWeight.w700
                )
            ),
            actions: <Widget>[
              IconButton(
                tooltip: 'Refresh',
                icon: Icon(Icons.refresh),
                onPressed: _handleRefresh,
              )
            ],
          ),
          body: StreamBuilder(
            stream: _postsController.stream,
            builder: (BuildContext context, AsyncSnapshot snapshot) {
              print('Has error: ${snapshot.hasError}');
              print('Has data: ${snapshot.hasData}');
              print('Snapshot Data ${snapshot.data}');

              if (snapshot.hasError) {
                return Text(snapshot.error);
              }

              if (snapshot.hasData) {
                return Column(
                  children: <Widget>[
                    Expanded(
                      child: Scrollbar(
                        child: RefreshIndicator(
                          onRefresh: _handleRefresh,
                          child: ListView.builder(
                            physics: const AlwaysScrollableScrollPhysics(),
                            itemCount: snapshot.data.length,
                            itemBuilder: (context, index) {
                              var post = snapshot.data[index];
                              return GestureDetector(
                                  onTap: () {
                                    print(post);
                                    Navigator.push(
                                      context,
                                      MaterialPageRoute(
                                          builder: (context) =>
                                              GridMerchantDetails(post)),
                                    );
                                  },
                                  child: Container(
                                      height: 160,
                                      margin: EdgeInsets.symmetric(vertical: 8.0, horizontal: 16.0),
                                      decoration: BoxDecoration(
                                          color: Colors.grey,
                                          image: DecorationImage(
                                              fit: BoxFit.cover,
                                              colorFilter: new ColorFilter.mode(
                                                  Colors.black.withOpacity(0.5),
                                                  BlendMode.darken),
                                              image: new NetworkImage(
                                                post['assets']['backgroundimage_url'] != null
                                                    ? 'https:' +
                                                    post['assets']['backgroundimage_url']
                                                    : 'https://images.unsplash.com/photo-1446844805183-9f5af45f89ee',
                                              ))),
                                      child: Column(
                                          mainAxisAlignment: MainAxisAlignment.center,
                                          children: <Widget>[
                                            FadeInImage.assetNetwork(
                                                image: post['assets']['logo_url'] != null
                                                    ? 'https:' + post['assets']['logo_url']
                                                    : 'https://images.unsplash.com/photo-1446844805183-9f5af45f89ee',
                                                placeholder: 'assets/images/transparent.png',
                                                width: 140,
                                                height: 140,
                                                fit: BoxFit.contain
                                            )
                                          ]
                                      )
                                  )
                              );
                           },     
                          ),
                        ),
                      ),
                    ),
                  ],
                );
              }

              if (!snapshot.hasData &&
                  snapshot.connectionState != ConnectionState.done) {
                return Text('No Merchants');
              }

              if (snapshot.connectionState != ConnectionState.done) {
                return Center(
                  child: CircularProgressIndicator(),
                );
              }


            },
          ),
        );
      }
    }

【问题讨论】:

    标签: flutter dart return widget


    【解决方案1】:

    如果您在StreamBuilder builder 的末尾添加一个备用return 以防万一所有其他方法都失败,则此问题应该得到解决:

    ...
    if (snapshot.connectionState != ConnectionState.done) {
      return Center(
        child: CircularProgressIndicator(),
      );
    }
    
    // This line at the very end after the last `if` statement
    return Center(child: Text('Data unavailable'));
    

    您还应该确保考虑到每一种失败的可能性。随着连接和可能的null数据

    【讨论】:

      【解决方案2】:

      如果快照没有数据,则不会返回任何内容,但 connectionState 已完成。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-03-26
        • 1970-01-01
        • 2019-05-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-06-16
        相关资源
        最近更新 更多