【发布时间】: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