flutter - 在 future builder 中使用列表构建器时 flutter 显示错误

标签 flutter dart

我在Future builder中使用ListBuilder在类中传递快照数据,并在listBuilder的帮助下以列表形式显示所有内容,但一切正常,但在列表末尾显示错误的同时也在UI中显示错误
我的密码

Widget build(BuildContext context) {
    double statusBarHeight = MediaQuery.of(context).padding.top;
    double width = MediaQuery.of(context).size.width;
    double height = MediaQuery.of(context).size.height;
    return 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 ListViewPosts(posts: snapshot);
              }
            }
            return Center(
              child: Text("Not Loaded Yet!!"),
            );
          }),
    );
  }

}

class ListViewPosts extends StatelessWidget {
  final posts;

  ListViewPosts({Key key, this.posts}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    double statusBarHeight = MediaQuery.of(context).padding.top;
    double width = MediaQuery.of(context).size.width;
    double height = MediaQuery.of(context).size.height;
    return Expanded(
      child: Container(
        child: ListView.builder(
            itemCount: posts.data.length,
            padding: const EdgeInsets.all(15.0),
            itemBuilder: (context, position) {
              return Container(
                height: height * 0.4,
                child: GestureDetector(
                  child: Center(
                    child: Card(
                      margin: EdgeInsets.zero,
                      clipBehavior: Clip.antiAlias,
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(30),
                      ),
                      elevation: 30,
                      child: Container(
                        child: Stack(
                          children: <Widget>[
                            Container(
                              width: width * 0.6,
                              height: height * 0.17,
                              decoration: BoxDecoration(
                                image: DecorationImage(
                                  image: AssetImage(
                                    'assets/images/61.jpg',
                                  ),
                                  fit: BoxFit.fill,
                                ),
                              ),
                            ),
                            Padding(
                              padding: EdgeInsets.only(top: height * 0.17),
                              child: Container(
                                height: height * 0.15,
                                color: Colors.white,
                                width: MediaQuery.of(context).size.width * 0.6,
                                child: Padding(
                                  padding: const EdgeInsets.only(
                                      top: 30, bottom: 7, left: 15, right: 15),
                                  child: Row(
                                    children: <Widget>[
                                      Flexible(
                                        child: Text(
                                          posts.data[position]['Description'],
                                          style: TextStyle(
                                              color: Color(0xff343A40),
                                              fontFamily: 'OpenSans',
                                              fontWeight: FontWeight.bold,
                                              fontSize: 15),
                                        ),
                                      ),
                                    ],
                                  ),
                                ),
                              ),
                            ),
                            Positioned.fill(
                              child: Align(
                                  alignment: Alignment.centerLeft,
                                  child: Container(
                                    margin: EdgeInsets.only(left: width * 0.02),
                                    child: ClipRRect(
                                      borderRadius:
                                          BorderRadius.all(Radius.circular(12)),
                                      child: Container(
                                        height: height * 0.08,
                                        width: width * 0.4,
                                        color: Color(0xff343A40),
                                        child: Column(
                                            mainAxisAlignment:
                                                MainAxisAlignment.center,
                                            crossAxisAlignment:
                                                CrossAxisAlignment.center,
                                            children: <Widget>[
                                              Text(
                                                posts.data[position]
                                                        ['CreatedOn']
                                                    .substring(0, 10),
                                                style: TextStyle(
                                                    fontSize: 20,
                                                    color: Color(0xffFFFFFF),
                                                    fontFamily: 'OpenSans',
                                                    fontWeight:
                                                        FontWeight.bold),
                                              ),
                                            ]),
                                      ),
                                    ),
                                  )),
                            ),
                          ],
                        ),
                      ),
                    ),
                  ),
                ),
              );
            }),
      ),
    );
  }
}
错误是方法'substring'在null上被调用。
接收者:null
尝试调用:substring(0,10)
enter image description here

最佳答案

问题是,您的subString之一返回空值。并且您尝试在“文本”窗口小部件中传递空值。
请尝试以下代码:

Text(
   posts.data[position]['CreatedOn'].substring(0, 10)??"Not available",
   style: TextStyle(
       fontSize: 20,
       color: Color(0xffFFFFFF),
       fontFamily: 'OpenSans',
       fontWeight:FontWeight.bold),
    ),

关于flutter - 在 future builder 中使用列表构建器时 flutter 显示错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63531845/

相关文章:

dart - 如何使用文本小部件设置多行文本?

unit-testing - Flutter Widget 测试无法正确模拟不同的屏幕尺寸

http - 从Flutter中的api获取后如何拆分文本数据

flutter - Dart:检查对象是否实现接口(interface)

android - 在 VS Code 上模拟 Android 时为 "Invalid Argument(s): Cannot find executable for null"- Mac OS X

json - Flutter GetX中如何使用observable方法处理复杂的API数据响应

swift - 将数据从flutter传递到原生IOS

dart - 在 Dart 中,给定可空类型 `T?` ,我如何获得不可空类型 `T`

websocket - Dart 升级客户端 Socket 到 WebSocket

printing - flutter 将小部件捕获到图像,然后创建 PDF 文件到打印机