小编Mad*_*Max的帖子

Flutter 错误:body 可能正常完成,导致返回 'null',但返回类型是潜在的不可为 null 的类型

我正在使用启用了空安全的新 dart 版本 <2.13.0-107.0.dev>。

有了这个任务列表:

  List<Task> tasks = [
    Task(name: 'find a way to live happy'),
    Task(name: 'Listen to music!!!'),
    Task(name: 'Live in the other world till ur power is off'),
  ];
Run Code Online (Sandbox Code Playgroud)

当我尝试在 ListView.builder 构造函数中使用它时:

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
      itemCount: tasks.length,
      itemBuilder: (context, index) {
         TaskTile(
          taskTitle: tasks[index].name,
          isChecked: tasks[index].isDone,
          checkboxCallback: (bool? checkboxState) {
            setState(() {

              tasks[index].toggleDone();
            });
          },
        );
      },
    );
  }
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

错误:主体可能正常完成,导致返回“空”,但返回类型可能是不可为空的类型。

以及运行日志中的此错误:

错误:必须返回非空值,因为返回类型“Widget”不允许为空。

有关更多信息,Task 类定义如下:

class Task {
  String name;
  bool isDone;

  Task({this.name …
Run Code Online (Sandbox Code Playgroud)

dart flutter flutter-listview dart-null-safety

4
推荐指数
2
解决办法
1万
查看次数

如何确保 future 及其所有子 future 调用在继续执行之前完成

我有一种方法将照片上传到 firebase 云存储,然后获取照片的下载 url,然后使用该 url 更新 firebase 数据库文档。当我使用uploadProfilePhoto(..)时,我的问题出现在ElevatedButton回调中。然后在setPersonalPhotoUrl()方法完成其工作并设置individualPhotoUrl之前执行代码。

我尝试使用whenComplete代替,但它不起作用。我的想法(如果没有错误的话)是uploadProfilePhoto(..).then正在完成其未来,但它没有考虑未来方法setPersonalPhotoUrl()的完成。我需要这方面的帮助。

声明的字段:

 UploadTask? uploadTask;
 String personalPhotoUrl = '';
Run Code Online (Sandbox Code Playgroud)

更新按钮:

                          ElevatedButton(
                          child: Text('Update Info'),
                          onPressed: () async {
                            await uploadProfilePhoto(profilePhotoFile).then((value) async {
                              // Create an instance of ServiceProvider
                              final SP = ServiceProvider(
                                id: currentUserUid!,
                                name: _controllerName.text.trim(),
                                last: _controllerLast.text.trim(),
                                mobile: _controllerMobile.text.trim(),
                                bio: _controllerBio.text.trim(),
                                photo: personalPhotoUrl, //problem here the value is ''
                                serviceId: _selectedServiceId!,
                                subServices: _selectedSubServicesList,
                              );

                              // Create or Update the …
Run Code Online (Sandbox Code Playgroud)

async-await dart google-cloud-storage flutter google-cloud-firestore

1
推荐指数
1
解决办法
82
查看次数