【问题标题】:Type 'Null' is not a subtype of type 'String' in type cast - Flutter类型“Null”不是类型转换中“String”类型的子类型 - Flutter
【发布时间】:2021-08-03 15:49:32
【问题描述】:

我在某处犯了一个错误,不幸的是,在显示结果后一切都应该正常,但它没有。

我尝试了很多方法,但不幸的是,仍然只有 CircularProgressIndicator 出现。一切似乎都已正确完成,但不幸的是缺少一些东西。

非常感谢任何帮助!

 class ApiService {
 final endPointUrl = "newsapi.org";
final client = http.Client();

  Future<List<Article>> getArticle() async {

final queryParameters = {
  'country': 'us',
  'category': 'business',
  'apiKey': 'd74673ec25dd468d94ab03035fa53ff7'
};

final uri = Uri.https(endPointUrl, '/v2/top-headlines', queryParameters);
final response = await client.get(uri);
Map<String, dynamic> json = jsonDecode(response.body);
print(response.body);
List<dynamic> body = json['articles'];
print(body);
List<Article> articles = body.map((dynamic item) => Article.fromJson(item)).toList();
print(articles);
return articles;

  }
}

和类:

class Article {
Source source;
 // String author;
  String title;
 String description;
 String url;
 String urlToImage;
 String publishedAt;


   Article(
     {required this.source,
    // required this.author,
    required this.title,
    required this.description,
    required this.url,
    required this.urlToImage,
    required this.publishedAt,
   });

factory Article.fromJson(Map<String, dynamic> json) {
return Article(
  source: Source.fromJson(json['source']),
  // author: json['author'] as String,
  title: json['title'] as String,
  description: json['description'] as String,
  url: json['url'] as String,
  urlToImage: json['urlToImage'] as String,
  publishedAt: json['publishedAt'] as String,
  );
}
 }


class Source {
  // String id;
   String name;

  Source({ required this.name});

  factory Source.fromJson(Map<String, dynamic> json) {
    return Source(
       // id: json['id'] as String,
        name: json['name']as String);
     }
   }

颤动代码:

  Widget mainArticles(BuildContext context) {
return FutureBuilder(
  future: client.getArticle(),
  builder: (BuildContext context, AsyncSnapshot<List<Article>> snapshot) {
    if (snapshot.hasData) {
      List<Article> articles = snapshot.data!;
      return ListView.builder(
          itemCount: articles.length,
          itemBuilder: (context, index) => customListTile(articles[index], context)
      );
    }
    return Center(
      child: CircularProgressIndicator(),
    );
  },
);
  }

【问题讨论】:

    标签: flutter dart flutter-layout


    【解决方案1】:

    两个选项:

    1. 要么在声明语句中使变量为空: String varName -> String? varName

    2. 在语句赋值中使用?? 运算符: urlToImage: json['urlToImage'] as String, -> urlToImage: json['urlToImage'] ?? "", //如果第一个值为null,则分配一个空字符串。

    【讨论】:

    • 虽然 2. 在技术上是正确的,但我认为实际上使用可空性在语义上更好地描述缺失值
    • @TmKVU 是的,我同意!
    【解决方案2】:

    您有json['urlToImage'] as String,但在您的数据中json['urlToImage'] 为空。因此,您应该将模型中的 urlToImage 更改为 String? 并将其转换为 String? 而不是 String 以使其可以为空。

    您可以在documentation 中阅读有关 null 安全性的更多信息

    【讨论】:

      猜你喜欢
      • 2022-07-06
      • 2023-03-03
      • 1970-01-01
      • 2023-01-26
      • 2021-11-28
      • 2021-08-23
      • 2022-11-02
      • 2020-11-24
      相关资源
      最近更新 更多