(以下 NoSuchMethodError 被抛出构建 Builder:)二传手'categoryName='被调用为空



我只是想从另一个类中获取数据,但我做不到。此外,也没有语法问题。目前应用程序中还没有API,但我将添加。当我在网上研究这个问题时,我找不到解决办法。通常每个人在使用API结构时都会遇到这样的错误。已经谢谢你的帮助了。祝你今天愉快

这是错误。

Performing hot restart...
Syncing files to device sdk gphone x86...
Restarted application in 1.400ms.
======== Exception caught by widgets library =======================================================
The following NoSuchMethodError was thrown building Builder:
The setter 'categoryName=' was called on null.
Receiver: null
Tried calling: categoryName="Ekonomi"
The relevant error-causing widget was: 
MaterialApp file:///C:/Users/gokbe/FlutterProjects/flutter_news_app/lib/main.dart:12:12
When the exception was thrown, this was the stack: 
#0      Object.noSuchMethod (dart:core-patch/object_patch.dart:54:5)
#1      getCategories (package:flutter_news_app/datas/data.dart:7:17)
#2      _HomePageState.initState (package:flutter_news_app/home_page.dart:18:18)
#3      StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4822:57)
#4      ComponentElement.mount (package:flutter/src/widgets/framework.dart:4659:5)
...
====================================================================================================
======== Exception caught by widgets library =======================================================
The following NoSuchMethodError was thrown building Builder:
The setter 'categoryName=' was called on null.
Receiver: null
Tried calling: categoryName="Ekonomi"
The relevant error-causing widget was: 
MaterialApp file:///C:/Users/gokbe/FlutterProjects/flutter_news_app/lib/main.dart:12:12
When the exception was thrown, this was the stack: 
#0      Object.noSuchMethod (dart:core-patch/object_patch.dart:54:5)
#1      getCategories (package:flutter_news_app/datas/data.dart:7:17)
#2      _HomePageState.initState (package:flutter_news_app/home_page.dart:18:18)
#3      StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4822:57)
#4      ComponentElement.mount (package:flutter/src/widgets/framework.dart:4659:5)
...
====================================================================================================

主页部分

import 'package:flutter/material.dart';
import 'package:flutter_news_app/datas/data.dart';
import 'package:flutter_news_app/models/category_model.dart';
import 'package:google_fonts/google_fonts.dart';
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
List<CategoryModel> categories;
@override
void initState() {
// TODO: implement initState
super.initState();
categories = getCategories();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
"Türkiye'nin",
style: GoogleFonts.inter(
fontWeight: FontWeight.w800,
color: Colors.red,
),
),
SizedBox(
width: 12,
),
Text(
"Gündemi",
style: GoogleFonts.inter(
fontWeight: FontWeight.w800,
color: Colors.white,
),
),
],
),
elevation: 12,
),
body: Container(
child: Column(
children: [
Container(
child: ListView.builder(
itemCount: categories.length,
shrinkWrap: true,
itemBuilder: (context, index) {
return CategoryTile(
imageUrl: categories[index].imageUrl,
categoryName: categories[index].categoryName,
);
},
),
),
],
),
),
);
}
}
class CategoryTile extends StatelessWidget {
final imageUrl, categoryName;
CategoryTile({this.imageUrl, this.categoryName});
@override
Widget build(BuildContext context) {
return Container(
child: Stack(
children: [
Image.network(
imageUrl,
width: 120,
height: 90,
),
],
),
);
}
}

数据部分

import 'package:flutter_news_app/models/category_model.dart';
List<CategoryModel> getCategories() {
List<CategoryModel> category;
CategoryModel categoryModel;
categoryModel.categoryName = "Ekonomi";
categoryModel.imageUrl =
"https://images.unsplash.com/photo-1486406146926-c627a92ad1ab?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1050&q=80";
category.add(categoryModel);
categoryModel = new CategoryModel();
categoryModel.categoryName = "Eğlence";
categoryModel.imageUrl =
"https://images.unsplash.com/photo-1518084823714-2f59a7315a39?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1050&q=80";
category.add(categoryModel);
categoryModel = new CategoryModel();
categoryModel.categoryName = "Sağlık";
categoryModel.imageUrl =
"https://images.unsplash.com/photo-1561328635-c1c6ad1753b0?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1257&q=80";
category.add(categoryModel);
categoryModel = new CategoryModel();
categoryModel.categoryName = "Bilim";
categoryModel.imageUrl =
"https://images.unsplash.com/photo-1446776811953-b23d57bd21aa?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1052&q=80";
category.add(categoryModel);
categoryModel = new CategoryModel();
categoryModel.categoryName = "Spor";
categoryModel.imageUrl =
"https://images.unsplash.com/photo-1614632537190-23e4146777db?ixid=MnwxMjA3fDB8MHxzZWFyY2h8MTA3fHxzcG9ydHN8ZW58MHx8MHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=500&q=60";
category.add(categoryModel);
categoryModel = new CategoryModel();
categoryModel.categoryName = "Teknoloji";
categoryModel.imageUrl =
"https://images.unsplash.com/photo-1488590528505-98d2b5aba04b?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1050&q=80";
category.add(categoryModel);
categoryModel = new CategoryModel();
return category;
}

类别型号零件

class CategoryModel {
String categoryName;
String imageUrl;
}

在设置属性之前需要进行初始化:

List<CategoryModel> getCategories() {
List<CategoryModel> category = [];
CategoryModel categoryModel = new CategoryModel();
categoryModel.categoryName = "Ekonomi";
[...]

最新更新