Open3

読者コミュニティ|Flutter Beginners Hands-on

takummatakumma

質問とか修正点があればこちらにお願いします。

ShuShu

初めまして。
hands-on4の_todosItemの変更の部分で

List<Todo> _todoItems = [
    Todo('英語の課題', Icons.description),
    Todo('牛乳を買う', Icons.local_grocery_store),
  ];

を変更したときに、このようなエラーがでてしまうのですが...

import 'package:flutter/material.dart';
import 'create_page.dart';

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  List<Todo> _todoItems = [
    Todo('英語の課題', Icons.description),
    Todo('牛乳を買う', Icons.local_grocery_store),
  ];

  void _addTodo(Todo todo) {
    setState(() {
      _todoItems.add(todo);
    });
  }

  void _deleteTodo(int title) {
    setState(() {
      _todoItems.removeAt(title);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(

        title: Text(widget.title),
      ),
      body: ListView.builder(
        itemCount: _todoItems.length,
        itemBuilder: (BuildContext context, int index) {
          return Card(
            child: Container(
              decoration: BoxDecoration(
                border: Border.all(width: 1.0, color: Colors.red),
              ),
              child: ListTile(
                leading: Icon(
                  _todoItems[index].icon,
                  size: 35.0,
                ),
                title: Text(_todoItems[index].title),
                trailing: IconButton(
                  icon: Icon(Icons.more_vert),
                  onPressed: () => showDialog(
                    context: context,
                    builder: (BuildContext context) => AlertDialog(
                      title: Text(_todoItems[index].title),
                      actions: [
                        IconButton(
                          icon: Icon(Icons.delete),
                          color: Colors.red,
                          onPressed: () {
                            _deleteTodo(index);
                            Navigator.pop(context);
                          },
                        ),
                      ],
                    )
                  ),
                ),
              ),
            ),
          );
        },
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () async {
          final String title = await Navigator.of(context).push(MaterialPageRoute(builder: (context) => CreatePage()));
          if(title != null && title != '') _addTodo(Todo(title, Icons.add));
        },
        tooltip: 'Add Todo',
        child: Icon(Icons.add),
      ),
    );
  }
}

class Todo {
  String title;
  IconData icon;
  Todo(this.title, this.icon);
}
takummatakumma

ご質問ありがとうございます。見た感じコードは問題なさそうなので、一度再実行(Restart)してみるといいかもしれません。再実行するときは、実行中に「▷」を再度クリックします。