【问题标题】:Flutter Throw or Return and Class ConstructionFlutter Throw or Return 和类构造
【发布时间】:2022-01-06 07:43:40
【问题描述】:

您好,我开始使用 AndroidStudio IDE 学习 Flutter。 当我跟随视频学习时,我会像在视频中一样更改我的代码。 但是存在差异,这种差异让我出错。

import 'package:flutterogrencitakip/models/student.dart';
import 'package:flutter/material.dart';

final Color darkBlue = Color.fromARGB(255, 18, 32, 47);

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: darkBlue),
        debugShowCheckedModeBanner: false,
        home: HomeScreen());
  }
}

class HomeScreen extends StatelessWidget {
  List<Student> students = [
    Student.withId(1, "Yusuf", "Erarslan", 95),
    Student.withId(2, "Yusufff", "Erarslassn", 35),
    Student.withId(3, "YusufffAAA", "ErAAAarslassn", 15)
  ];
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text("Öğrenci Takip Sistemi"),
        ),
        body: buildBody());
  }

  Widget buildBody() {
   return Column(
     children:<Widget>[
       Expanded(
         child: ListView.builder(
           itemCount: students.length,
             itemBuilder: (BuildContext context, int index){
             return Text(students[index].firstName);
             }),
       )
     ],
   );

  }
}

即使我在模型文件夹中有 student.dart,此代码也会让我出错。 这是输出。

lib/main.dart:40:42: Error: The argument type 'String?' can't be assigned to the parameter type 'String' because 'String?' is nullable and 'String' isn't.
             return Text(students[index].firstName);

这是因为我使用 return 而不是 throw 或者我的 void 主类和 myApp 类的定义错误? 因为就是这样。

void main(){
const MyApp bla bla bla i can't remember exactly
}

截图

我的项目文件夹:

我的问题:

我更改的区域:

我的学生班级:

class Student{
  int? id;
  String? firstName;
  String? lastName;
  int? grade;
  String? status;

  (String firstName, String lastName, int grade){
    this.firstName= firstName;
    this.lastName= lastName;
    this.grade  = grade;
  }

  //named constructor
  Student.withId(int id,String firstName, String lastName, int grade){
    this.id=id;
    this.firstName= firstName;
    this.lastName= lastName;
    this.grade  = grade;
  }
}

【问题讨论】:

  • 您的Student 类是如何定义的?我猜firstName 被定义为具有String? 类型。
  • 可以包含Student模型类吗?
  • 我包括了我的学生课程和截图

标签: flutter android-studio dart


【解决方案1】:

在您的模型类 Student 上定义为可为空的 String? firstName;

但是 Text 小部件需要一个真实的文本,而不是 null。所以我会建议检查 null 然后分配它,或者你可以提供这样的默认值:

Text(students[index].firstName ?? '');

查看更多关于null-safety

如果您绝对确定您的可空字符串实际上一个值,您可以在末尾添加!

Text(students[index].firstName!)

Bang! 操作符只是告诉 Dart,即使我们定义了一些变量为 Nullable 类型,它也绝对不会为 null。 更多关于bang operator question

【讨论】:

  • Text(students[index].firstName!) 会让编译器高兴,并让用户问他们的下一个 SO 问题,为什么他们有这个运行时错误。
  • 确实是这样,所以我建议先检查 null,
  • 我知道,我没有投反对票,因为您确实添加了更好的版本,只是很多人没有读那么远,他们只看到! 使他们的错误消失了然后他们完全惊讶于他们有运行时错误:(
  • 随意编辑答案。
  • 谢谢哥们。
【解决方案2】:

这里的问题是Text 小部件接受String(不接受空值)而不是String?(可空值)的参数类型。

一个解决方案是捕获您提供的字符串(在本例中为名字)是否为空

return Text(students[index].firstName ?? '');

如果students[index].firstNamenull,上述代码将输出''

【讨论】:

    【解决方案3】:

    发生这种情况是因为变量 firstName? 可以为空。这意味着在某个时候它可能是nullText() 只接受String,所以程序知道在某些时候它可能会得到一个null 而不是String 然后抛出错误。

    在您的模型中使变量不可为空,它应该可以工作。

    firstName 而不是firstName?

    请注意,如果您这样做,变量firstName 将始终需要值。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-28
    • 1970-01-01
    • 2020-12-20
    • 2019-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-03
    相关资源
    最近更新 更多