(以下 NoSuchMethodError 在构建 Builder 时被抛出:)setter 'categoryName=' 在 null 上被调用

时间:2021-05-03 09:50:39

标签: flutter dart

我只是想从另一个类中获取数据,但我不能。也没有语法问题。目前应用程序中没有 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;
}

1 个答案:

答案 0 :(得分:0)

设置属性前需要初始化:

List<CategoryModel> getCategories() {
  List<CategoryModel> category = [];
  CategoryModel categoryModel = new CategoryModel();

  categoryModel.categoryName = "Ekonomi";
  [...]