我是 flutter 的新手,所以我正在試驗它,我嘗試在 vs 代碼中創建一個自定義類和該類的建構式,我不斷地收到關于該類的建構式的錯誤訊息。錯誤是這樣寫的“不可為空的實體欄位作者(這是類的屬性之一的名稱)必須被初始化idk這是什么錯誤我已經嘗試了各種在線資源但只是徒勞地回傳。看看代碼和識別錯誤的幫助將非常有幫助。
代碼:
import 'package:flutter/material.dart';
import 'quote_class.dart';
void main() {
runApp(const MaterialApp(
home: Home(),
debugShowCheckedModeBanner: false,
));
}
class Home extends StatefulWidget {
const Home({Key? key}) : super(key: key);
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
List<Quotes> quotes = [
Quotes(
"Be yoursefl cause everyone else is taken","Oscar Wilde"),
Quotes(
"I have nothing to declare except my genius", "Einstien"),
Quotes(
"The truth is rarely pure and never simple", "Priyanka"),
Quotes(
"Trust those who are still in the search of truth and not those who has already found one","Hey")
];
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.blue[400],
appBar: AppBar(
title: const Text("Awesome Quotes"),
centerTitle: true,
backgroundColor: Colors.purple[200],
),
body: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: quotes
.map((quote) => Text('${quote.text} - ${quote.author}'))
.toList(),
));
}
}
出現錯誤的自定義類的代碼,我正在嘗試使用上述 main.dart 檔案中的自定義類
class Quotes {
String text;
String author;
Quotes(String text, String author) {
this.text = text;
this.author = author;
}
}
錯誤資訊:
lib/quote.dart:5:16: Error: The parameter 'text' can't have a value of 'null' because of its type 'String', but the implicit default value is 'null'.
Try adding either an explicit non-'null' default value or the 'required' modifier.
Quotes({this.text, this.author});
^^^^
lib/quote.dart:5:27: Error: The parameter 'author' can't have a value of 'null' because of its type 'String', but the implicit default value is 'null'.
Try adding either an explicit non-'null' default value or the 'required' modifier.
Quotes({this.text, this.author});
^^^^^^
uj5u.com熱心網友回復:
您可以在Quotes模型中使用空安全,只需在屬性名稱前加一個問號
import 'package:flutter/material.dart';
void main() {
runApp(const MaterialApp(
home: Home(),
debugShowCheckedModeBanner: false,
));
}
class Home extends StatefulWidget {
const Home({Key? key}) : super(key: key);
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
List<Quotes> quotes = [
Quotes(
"Be yoursefl cause everyone else is taken","Oscar Wilde"),
Quotes(
"I have nothing to declare except my genius", "Einstien"),
Quotes(
"The truth is rarely pure and never simple", "Priyanka"),
Quotes(
"Trust those who are still in the search of truth and not those who has already found one","Hey")
];
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.blue[400],
appBar: AppBar(
title: const Text("Awesome Quotes"),
centerTitle: true,
backgroundColor: Colors.purple[200],
),
body: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: quotes
.map((quote) => Text('${quote.text} - ${quote.author}'))
.toList(),
));
}
}
class Quotes {
String? text;
String? author;
Quotes(String text, String author) {
this.text = text;
this.author = author;
}
}
輸出:

uj5u.com熱心網友回復:
飛鏢零安全不考慮建構式的主體。太多的混亂,太多的機會或太難以確定。在 dart 中,您可以直接告訴編譯器您希望引數去哪里,而不必在正文中將其拼寫出來。
這是您想要的課程:
class Quotes {
final String text;
final String author;
const Quotes(this.text, this.author);
}
uj5u.com熱心網友回復:
看起來您的專案正在以聲音零安全性運行。
所以,對于你的班級
class Quotes {
String text;
String author;
Quotes(String text, String author) {
this.text = text;
this.author = author;
}
}
欄位 text 和 author 不能為空(將它們宣告為帶問號的字串?將使它們可以為空)并可能修復錯誤。但是,如果您希望它們不可為空,您可以按原樣宣告它們并在其前面添加一個 'late' 關鍵字,這將使文本/作者值在準備好之前為空。
課程如下
class Quotes {
late String text;
late String author;
Quotes(String text,String author) {
this.text = text;
this.author = author;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/331386.html
上一篇:我在我的資料庫'1631363150'中有這種格式的日期,我想將其更改為dd-mm-yyyy(DART)
下一篇:來自鏈接/URL的Dart子字串
