Flutter (以下NoSuchMethodError被抛出building Builder:)setter';类别名称=';被调用为空
我只是想从另一个类中获取数据,但我做不到。此外,也没有语法问题。目前应用程序中还没有API,但我会添加。当我在互联网上研究这个问题时,我找不到解决办法。通常每个人在使用API结构时都会遇到这样的错误。已经谢谢你的帮助了。祝你今天愉快。 这里是错误Flutter (以下NoSuchMethodError被抛出building Builder:)setter';类别名称=';被调用为空,flutter,dart,Flutter,Dart,我只是想从另一个类中获取数据,但我做不到。此外,也没有语法问题。目前应用程序中还没有API,但我会添加。当我在互联网上研究这个问题时,我找不到解决办法。通常每个人在使用API结构时都会遇到这样的错误。已经谢谢你的帮助了。祝你今天愉快。 这里是错误 Performing hot restart... Syncing files to device sdk gphone x86... Restarted application in 1.400ms. ======== Exception caug
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,
),
],
),
);
}
}
您需要在设置属性之前进行初始化:
List<CategoryModel> getCategories() {
List<CategoryModel> category = [];
CategoryModel categoryModel = new CategoryModel();
categoryModel.categoryName = "Ekonomi";
[...]
List getCategories(){
列表类别=[];
CategoryModel CategoryModel=新的CategoryModel();
categoryModel.categoryName=“Ekonomi”;
[...]
class CategoryModel {
String categoryName;
String imageUrl;
}
List<CategoryModel> getCategories() {
List<CategoryModel> category = [];
CategoryModel categoryModel = new CategoryModel();
categoryModel.categoryName = "Ekonomi";
[...]