【问题标题】:Flutter Null Safety Error in snapshot.data.lengthsnapshot.data.length 中的 Flutter Null 安全错误
【发布时间】:2021-11-25 17:14:34
【问题描述】:

当我将项目从无空安全更改为空安全时,我收到此错误。 我得到的错误是:

“属性‘length’不能被无条件访问,因为接收者可以是‘null’。尝试使访问有条件(使用'?.')或对目标添加一个空检查('! ')"

itemCount: snapshot.data.length 行中,所以我将行改为itemCount: snapshot.data!.length。现在我得到了错误

没有为“对象”类型定义吸气剂“长度”。尝试导入定义“length”的库,将名称更正为现有 getter 的名称,或者定义一个名为“length”的 getter 或字段。

我的代码是这样的:

import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:movie_app/constants/myUrl.dart';

class SearchPost extends SearchDelegate<String?> {
  List<dynamic>? list;
  SearchPost({this.list});

  Future showAllPost() async {
    var url = "$baseUrl/searchPost.php";
    var response = await http.post(
      Uri.parse(url),
      body: {'title': query},
    );
    if (response.statusCode == 200) {
      var jsonData = json.decode(response.body);
      print(jsonData);
      return jsonData;
    }
  }

  @override
  List<Widget> buildActions(BuildContext context) {
    return [
      IconButton(
        onPressed: () {
          query = "";
          showSuggestions(context);
        },
        icon: Icon(
          Icons.close,
        ),
      ),
    ];
  }

  @override
  Widget buildLeading(BuildContext context) {
    return IconButton(
      icon: Icon(Icons.arrow_back),
      onPressed: () {
        close(context, null);
      },
    );
  }

  @override
  Widget buildResults(BuildContext context) {
    return FutureBuilder(
      future: showAllPost(),
      builder: (context, snapshot) {
        if (snapshot.hasData) {
          return ListView.builder(
          itemCount: snapshot.data!.length,
          itemBuilder: (context, index) {
          var list = snapshot.data[index];
              return Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisAlignment: MainAxisAlignment.start,
                children: <Widget>[
                  Padding(
                    padding: EdgeInsets.all(8.0),
                    child: Text(
                      list['title'],
                      style: TextStyle(
                        fontSize: 22,
                        fontFamily: 'Nasalization',
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                  ),
                  Center(
                    child: Container(
                      child: Image.network(
                        '$baseUrl/uploads/${list['image']}',
                        height: 250,
                      ),
                    ),
                  ),
                  Padding(
                    padding: EdgeInsets.all(8),
                    child: Text(
                      list['body'] == null ? "" : list['body'],
                      style: TextStyle(
                        fontSize: 20,
                      ),
                    ),
                  ),
                  Row(
                    children: <Widget>[
                      Padding(
                        padding: EdgeInsets.all(8),
                        child: Text(
                          "by " + list['author'],
                          style: TextStyle(
                            fontSize: 16,
                            color: Colors.grey,
                          ),
                        ),
                      ),
                      SizedBox(
                        width: 5,
                      ),
                      Padding(
                        padding: const EdgeInsets.all(8.0),
                        child: Text(
                          "Posted on : " + list['post_date'],
                          style: TextStyle(
                            fontSize: 16,
                            color: Colors.grey,
                          ),
                        ),
                      ),
                    ],
                  ),
                  Padding(
                    padding: const EdgeInsets.all(8.0),
                    child: Text(
                      "Comments Area",
                      style: TextStyle(
                          fontSize: 20,
                          fontWeight: FontWeight.bold,
                          fontFamily: 'Nasalization'),
                    ),
                  ),
                  Padding(
                    padding: const EdgeInsets.all(8.0),
                    child: TextField(
                      decoration: InputDecoration(
                        labelText: "Enter Comments",
                      ),
                    ),
                  ),
                  Padding(
                    padding: const EdgeInsets.all(8.0),
                    child: Center(
                      child: MaterialButton(
                        color: Colors.blue,
                        child: Text("Publish"),
                        onPressed: () {
                          //Code to publish comment
                        },
                      ),
                    ),
                  ),
                ],
              );
            },
          );
        }
        return CircularProgressIndicator();
      },
    );
  }

jsonData 的响应是这样的:

[{id: 18, title: Movie Title, body: Movie Body, author: Murali, post_date: 28/09/2021 08:46, category_name: Movies, image: image_picker4111148115016181882.jpg, create_date: 27/09/2021, comments: 4, total_like: 4, movie_url: https://www.testmovie.com/movie.mp4}]

这在没有空安全性的项目中运行良好。如何解决这个错误?

【问题讨论】:

  • 将快照更改为 AsyncSnapshot 快照并尝试

标签: flutter dart flutter-web dart-null-safety


【解决方案1】:

尝试用这个替换snapshot.data!.length snapshot.data?.length ??0 它应该工作

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-25
    • 2021-04-04
    • 2021-11-26
    • 1970-01-01
    • 2021-11-03
    • 1970-01-01
    • 2021-09-24
    相关资源
    最近更新 更多