type 'int' is not a subtype of type 'String' in Flutter

Question

Asked by Fabboy on November 12, 2021 (source).

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 :)

Answer

Question answered by Ravindra S (source).

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(),
              ),
            ),
          ),
        );
      }),

Your result screen-> image

DART FLUTTER
SHARE: