I'm making a Todolist as first project with flutter and now I have this error:
Problem 1
type 'int' is not a subtype of type 'String'
Problem 2
I have also problem with line: 18
in the tutorial he says: List todos = List();
but this isn't working
My code:
import 'package:flutter/material.dart';
void main() => runApp(MaterialApp(
theme: ThemeData(
brightness: Brightness.light,
primaryColor: Colors.blue,
colorScheme: ColorScheme.fromSwatch().copyWith(secondary: Colors.orange)
),
home: const TodoApp()
));
class TodoApp extends StatefulWidget{
const TodoApp({Key? key}) : super(key: key);
@override
_TodoState createState() => _TodoState();
}
class _TodoState extends State<TodoApp>{
List todos = List.filled(3, 0, growable: true);
void initSate(){
super.initState();
todos.add("Item1");
todos.add("Item2");
todos.add("Item3");
todos.add("Item4");
}
@override
Widget build(BuildContext context){
return Scaffold(
appBar: AppBar(
title: const Text("TodoApp"),
),
body: ListView.builder(
itemCount: todos.length,
itemBuilder: (BuildContext context, int index){
return Dismissible(
key: Key(todos[index]),
child: Card(
child: ListTile(
title: Text(todos[index]),
),
));
}),
);
}
}
I would like to learn something. Thank you for helping :)
CodePudding user response:
Try below code hope its helpful to you. use toString() for index
ListView.builder(
itemCount: todos.length,
itemBuilder: (BuildContext context, int index) {
return Dismissible(
key: Key(
todos[index].toString(),
),
child: Card(
child: ListTile(
title: Text(
todos[index].toString(),
),
),
),
);
}),
CodePudding user response:
The problem is simply because you're incorrectly initialized your variables with:
List todos = List.filled(3, 0, growable: true);
which will initialized your variables with integer.
The same problem could be easily avoided in the future by creating your list with specific type, like List<String>
.
So, change your code to the following:
// Using dynamic list
List todos = List.filled(3, "", growable: true);
// this will initialized todos => ["", "", ""]
or
// use list string
List<String> todos = List.filled(3, "", growable: true);
// this will initialized todos => ["", "", ""]
When removing item with dismissable, you need to add onDismissed
property for Dismissable
.
Here the complete working code with all the errors corrected:
import 'package:flutter/material.dart';
void main() => runApp(MaterialApp(
theme: ThemeData(
brightness: Brightness.light,
primaryColor: Colors.blue,
colorScheme:
ColorScheme.fromSwatch().copyWith(secondary: Colors.orange)),
home: const TodoApp()));
class TodoApp extends StatefulWidget {
const TodoApp({Key? key}) : super(key: key);
@override
_TodoState createState() => _TodoState();
}
class _TodoState extends State<TodoApp> {
//List<String> todos = List.filled(0, "", growable: true);
// Use empty list instead List.filled
List<String> todos = [];
// Need to override initState to adding items.
@override
void initState() {
todos.add("Item1");
todos.add("Item2");
todos.add("Item3");
todos.add("Item4");
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("TodoApp"),
),
body: ListView.builder(
itemCount: todos.length,
itemBuilder: (BuildContext context, int index) {
// use item for each todo by index.
final item = todos[index];
return Dismissible(
// Add onDismissed property to properly delete an item
onDismissed: (direction) {
// Remove the item from the data source.
setState(() {
todos.removeAt(index);
});
// Then show a snackbar.
ScaffoldMessenger.of(context)
.showSnackBar(SnackBar(content: Text('$item dismissed')));
},
key: Key(item),
child: Card(
child: ListTile(
title: Text(item),
),
));
}),
);
}
}
Result:
See https://api.dart.dev/stable/2.14.4/dart-core/List/List.filled.html for details.