尝试创建笔记应用程序时出错 (Flutter)
Errors trying to create a notes app (Flutter)
我是堆栈溢出的新手,所以请随时告诉我任何我遗漏的内容,这将帮助大家解决我的问题。
我正在使用 Dart 和 Flutter 在 Android Studio 上创建一个基本的笔记应用程序。
我 运行 关注的问题是
- 我不断收到红色错误屏幕,显示“类型 'init' 不是类型 'string' 的子类型”
- 以及当我尝试执行 'List todo = List();' 时,我得到“启用空安全时默认 'List' 构造函数不可用。”
我一直在努力解决这个问题,但 stuck/have 已经为此苦苦挣扎了一段时间。希望任何 advice/how 解决这个问题。
这是我的代码,非常感谢任何帮助:
import 'dart:ffi';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:units/main.dart';
import '../views/dreams_view.dart';
import '../presenter/dreams_presenter.dart';
import 'home_screen.dart';
class Log_Page extends StatefulWidget {
final UNITSPresenter presenter;
Log_Page(this.presenter, {required Key? key, required this.title}) : super(key: key);
final String title;
@override
_LogPageState createState() => _LogPageState();
}
void main ()=> runApp(MyApp());
class _LogPageState extends State<Log_Page> {
@override
Widget build(BuildContext context) {
// TODO: implement build
return MaterialApp(
home: Home(),
debugShowCheckedModeBanner: false,
theme: ThemeData(
primaryColor: Color(0xff2B2D2D),
scaffoldBackgroundColor: Color(0xff2B2D2D)
)
);
}
}
class Home extends StatefulWidget {
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
String input = " ";
List todo = List(); //todo = diary/log
//void initState (){
//todo.add("cycle");
//super.initState();
//}
@override
Widget build (BuildContext context) {
return SafeArea(
child: Scaffold(
appBar: AppBar(
elevation: 0.0,
centerTitle: true,
title: Text("Diary/Log",
style: TextStyle(
color: Colors.white,
fontSize: 35,
fontStyle: FontStyle.italic,
letterSpacing: 2,
),
),
),
floatingActionButton: FloatingActionButton(
backgroundColor: Colors.white,
child: Icon(Icons.add,color: Colors.red [500],size: 35, ),
onPressed: (){
showDialog (
context: context,
builder: (BuildContext context){
return AlertDialog(
//backgroundColor: Color(0xffF48C8C),
title: Text("Add Diary/Log"),
content: TextField(
decoration: InputDecoration(
hintText: "Diary/Log"
),
onChanged: (String value){
input = value;
},
),
actions: [
FlatButton(
onPressed: ()
{
setState(() {
todo.add(input);
});
Navigator.of(context).pop();
},
child: Text("Add",style: TextStyle(color: Colors.black),)
)
],
);
});
},
),
body: Padding (
padding: EdgeInsets.all(5),
child: ListView.builder(
itemCount: todo.length,
itemBuilder: (BuildContext context,int index){
return Dismissible(
key: Key(todo[index]),
child: Card(
elevation: 4,
margin: EdgeInsets.all(8),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(16),
),
child: ListTile(
title: Text(todo[index],
style: TextStyle(
color: Colors.black,
),),
trailing: IconButton(
icon: Icon(Icons.delete_forever_rounded, color: Colors.red,),
onPressed: ()
{
setState(() {
todo.removeAt(index);
});
},
),
) ,
)
);
}
),
),
),
);
}
}
想要一个简单的笔记application/page。
尝试指定列表 type
,建议使用 []
初始化空列表,因为不推荐使用 List()
初始化。
List<String> todo = [];
你的数组声明不正确,试试这个
List<String> todo = [];
所以你的代码中的问题是你想要初始化一个你使用了这个代码的列表:
List todo = List();
你所做的是构建列表的 class。为了声明列表,你只需要写:
列出待办事项 = [];
如果你想使用以前的方法,你需要记住这个构造函数不能在 null-safe 代码中使用。使用 List.filled 创建一个 non-empty 列表。这需要一个填充值来初始化列表元素。要创建空列表,请对可增长列表使用 [] 或对固定长度列表使用 List.empty(或者可增长性由 run-time 确定)。
我是堆栈溢出的新手,所以请随时告诉我任何我遗漏的内容,这将帮助大家解决我的问题。
我正在使用 Dart 和 Flutter 在 Android Studio 上创建一个基本的笔记应用程序。 我 运行 关注的问题是
- 我不断收到红色错误屏幕,显示“类型 'init' 不是类型 'string' 的子类型”
- 以及当我尝试执行 'List todo = List();' 时,我得到“启用空安全时默认 'List' 构造函数不可用。”
我一直在努力解决这个问题,但 stuck/have 已经为此苦苦挣扎了一段时间。希望任何 advice/how 解决这个问题。
这是我的代码,非常感谢任何帮助:
import 'dart:ffi';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:units/main.dart';
import '../views/dreams_view.dart';
import '../presenter/dreams_presenter.dart';
import 'home_screen.dart';
class Log_Page extends StatefulWidget {
final UNITSPresenter presenter;
Log_Page(this.presenter, {required Key? key, required this.title}) : super(key: key);
final String title;
@override
_LogPageState createState() => _LogPageState();
}
void main ()=> runApp(MyApp());
class _LogPageState extends State<Log_Page> {
@override
Widget build(BuildContext context) {
// TODO: implement build
return MaterialApp(
home: Home(),
debugShowCheckedModeBanner: false,
theme: ThemeData(
primaryColor: Color(0xff2B2D2D),
scaffoldBackgroundColor: Color(0xff2B2D2D)
)
);
}
}
class Home extends StatefulWidget {
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
String input = " ";
List todo = List(); //todo = diary/log
//void initState (){
//todo.add("cycle");
//super.initState();
//}
@override
Widget build (BuildContext context) {
return SafeArea(
child: Scaffold(
appBar: AppBar(
elevation: 0.0,
centerTitle: true,
title: Text("Diary/Log",
style: TextStyle(
color: Colors.white,
fontSize: 35,
fontStyle: FontStyle.italic,
letterSpacing: 2,
),
),
),
floatingActionButton: FloatingActionButton(
backgroundColor: Colors.white,
child: Icon(Icons.add,color: Colors.red [500],size: 35, ),
onPressed: (){
showDialog (
context: context,
builder: (BuildContext context){
return AlertDialog(
//backgroundColor: Color(0xffF48C8C),
title: Text("Add Diary/Log"),
content: TextField(
decoration: InputDecoration(
hintText: "Diary/Log"
),
onChanged: (String value){
input = value;
},
),
actions: [
FlatButton(
onPressed: ()
{
setState(() {
todo.add(input);
});
Navigator.of(context).pop();
},
child: Text("Add",style: TextStyle(color: Colors.black),)
)
],
);
});
},
),
body: Padding (
padding: EdgeInsets.all(5),
child: ListView.builder(
itemCount: todo.length,
itemBuilder: (BuildContext context,int index){
return Dismissible(
key: Key(todo[index]),
child: Card(
elevation: 4,
margin: EdgeInsets.all(8),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(16),
),
child: ListTile(
title: Text(todo[index],
style: TextStyle(
color: Colors.black,
),),
trailing: IconButton(
icon: Icon(Icons.delete_forever_rounded, color: Colors.red,),
onPressed: ()
{
setState(() {
todo.removeAt(index);
});
},
),
) ,
)
);
}
),
),
),
);
}
}
想要一个简单的笔记application/page。
尝试指定列表 type
,建议使用 []
初始化空列表,因为不推荐使用 List()
初始化。
List<String> todo = [];
你的数组声明不正确,试试这个
List<String> todo = [];
所以你的代码中的问题是你想要初始化一个你使用了这个代码的列表:
List todo = List();
你所做的是构建列表的 class。为了声明列表,你只需要写:
列出待办事项 = [];
如果你想使用以前的方法,你需要记住这个构造函数不能在 null-safe 代码中使用。使用 List.filled 创建一个 non-empty 列表。这需要一个填充值来初始化列表元素。要创建空列表,请对可增长列表使用 [] 或对固定长度列表使用 List.empty(或者可增长性由 run-time 确定)。