简体   繁体   English

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

[英](The following NoSuchMethodError was thrown building Builder:) The setter 'categoryName=' was called on null

I just trying to take the datas from the another class but I couldn't.我只是想从另一个 class 中获取数据,但我做不到。 Also there is no syntax issue.也没有语法问题。 For now there is no API in app but I will add.目前应用程序中没有 API 但我会添加。 When I researched this problem on the internet, I could not find a solution.当我在互联网上研究这个问题时,我找不到解决方案。 Usually everyone has experienced such an error while using the API structure.通常大家在使用API结构的时候都遇到过这样的错误。 Already thanks for help.已经感谢您的帮助。 Have a nice day.祝你今天过得愉快。

Here is the error.这是错误。

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)
...
====================================================================================================

Home Page Part主页部分

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,
          ),
        ],
      ),
    );
  }
}

Data Part数据部分

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;
}

Category Model Part类别 Model 零件

class CategoryModel {
  String categoryName;
  String imageUrl;
}

You need to initialize before set attributes:您需要在设置属性之前进行初始化:

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在构建 Builder(dirty) 时引发了以下 NoSuchMethodError:在 null 上调用了 getter 'data'。 接收方:null 尝试调用:数据 - The following NoSuchMethodError was thrown building Builder(dirty): The getter 'data' was called on null. Receiver: null Tried calling: data 在构建 Builder(dirty) 时引发了以下 NoSuchMethodError:在 null 上调用了方法“&gt;=”。 接收器:null 相关的错误原因是: - The following NoSuchMethodError was thrown building Builder(dirty): The method '>=' was called on null. Receiver: null The relevant error-causing was: 在构建 Builder 时抛出了以下 NoSuchMethodError:getter &#39;email&#39; 在 null 上被调用。 在火力基地 - The following NoSuchMethodError was thrown building Builder: The getter 'email' was called on null. in firebase 在构建 MessageBubble(dirty) 时引发了以下 NoSuchMethodError:在 null 上调用了 getter 'millisecondsSinceEpoch' - The following NoSuchMethodError was thrown building MessageBubble(dirty): The getter 'millisecondsSinceEpoch' was called on null 在构建 Home() 时引发了以下 NoSuchMethodError:在 null 上调用了方法“&gt;”。 接收方:null 尝试呼叫:&gt;(1) - The following NoSuchMethodError was thrown building Home(): The method '>' was called on null. Receiver: null Tried calling: >(1) 抛出了另一个异常:NoSuchMethodError: The setter &quot;wastedate=&quot; was called on null - Another exception was thrown: NoSuchMethodError: The setter "wastedate=" was called on null 我该如何解决这个问题? 构建时引发了以下 NoSuchMethodError:在 null 上调用了方法“[]”。 接收方:null 尝试调用:[](0) - How do i fix this issue? The following NoSuchMethodError was thrown building: The method '[]' was called on null. Receiver: null Tried calling: [](0) 在构建 CatMealsScreen 时引发了以下 NoSuchMethodError - The following NoSuchMethodError was thrown building CatMealsScreen 在构建 Builder 时抛出了以下断言:在构建过程中调用了 setState() 或 markNeedsBuild()? - The following assertion was thrown building Builder: setState() or markNeedsBuild() called during build? NoSuchMethodError:在null上调用了Setter“ movies =” - NoSuchMethodError : The Setter “movies=” was called on null
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM