I’m new to stack overflow so feel free to let me know anything that I am missing to include that will help you all in solving my issue.
I am creating a basic notes application on Android Studio using Dart and Flutter.
The issues that I am running into are
- I keep getting a red error screen that says "Type ‘init’ is not a subtype of type ‘string’"
- As well as to when I try to do ‘List todo = List();’ I get "The default ‘List’ constructor isn’t available when null safety is enabled."
I’ve been trying to resolve this issue, but been stuck/have been struggling with it for a while now. Was hoping on any advice/how to go about this issue.
Here is my Code, any help is much appreciated:
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);
});
},
),
) ,
)
);
}
),
),
),
);
}
}
Wanted a simple notes application/page.
3
Answers
Try to specify the list
type
and it is recommended to initialize empty list with[]
because initialize withList()
is deprecated.You declared your array incorrectly, try this
So the problem in your code is that you wanted to initialize a List for which you used this code:
what you did was you constructed the class of list. In order to declare the list you just have to write :
List todo = [];
if you want to use the previous method you need to keep in mind that This constructor cannot be used in null-safe code. Use List.filled to create a non-empty list. This requires a fill value to initialize the list elements with. To create an empty list, use [] for a growable list or List.empty for a fixed length list (or where growability is determined at run-time).