質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Flutter

Flutterは、iOSとAndroidのアプリを同じコードで開発するためのフレームワークです。オープンソースで開発言語はDart。双方のプラットフォームにおける高度な実行パフォーマンスと開発効率を提供することを目的としています。

Q&A

1回答

1546閲覧

flutter : ページの画面遷移が上手くいかない😢

suuu_naru

総合スコア0

Flutter

Flutterは、iOSとAndroidのアプリを同じコードで開発するためのフレームワークです。オープンソースで開発言語はDart。双方のプラットフォームにおける高度な実行パフォーマンスと開発効率を提供することを目的としています。

0グッド

0クリップ

投稿2022/09/11 08:26

前提

TODOアプリを作成しています。

onPressedを使って画面遷移したいのですが、エラー表示になってしまいます。
エラー文を読んでも解決できない状態です。

実現したいこと

onPressedを使って画面遷移したい
todo_list_pageからedit_todo_pageへ画面遷移

発生している問題・エラーメッセージ

error: The argument type 'void Function()' can't be assigned to the parameter type 'void Function(BuildContext)?'. (argument_type_not_assignable at [flutter_training_app1] lib/presentation/todo_list/todo_list_page.dart:44) error: The values in a const list literal must be constants. (non_constant_list_element at [flutter_training_app1] lib/presentation/todo_list/todo_list_page.dart:44) error: Invalid constant value. (invalid_constant at [flutter_training_app1] lib/presentation/todo_list/todo_list_page.dart:44) error: The method 'edit_todo_page' isn't defined for the type 'TodoListPage'. (undefined_method at [flutter_training_app1] lib/presentation/todo_list/todo_list_page.dart:48)

該当のソースコード

dart言語 : todo_list_page

import 'package:flutter/material.dart'; import 'package:flutter_slidable/flutter_slidable.dart'; import 'package:flutter_training_app1/domin/todo.dart'; import 'package:flutter_training_app1/presentation/add_todo/add_todo_page.dart'; import 'package:flutter_training_app1/presentation/todo_list/todo_list_model.dart'; import 'package:provider/provider.dart'; class TodoListPage extends StatelessWidget { @override Widget build(BuildContext context) { return ChangeNotifierProvider<TodoListModel>( create: (_) => TodoListModel()..fetchTodos(), child: Scaffold( appBar: AppBar( title: Text('todo list page'), ), body: Consumer<TodoListModel>( builder: (context, model, child) { final todos = model.todos; final listTiles = todos .map( (todo) => Slidable( child: ListTile( title: Text(todo.title), ), key: const ValueKey(0), // The start action pane is the one at the left or the top side. endActionPane: ActionPane( // A motion is a widget used to control how the pane animates. motion: const ScrollMotion(), // A pane can dismiss the Slidable. dismissible: DismissiblePane(onDismissed: () {}), // All actions are defined in the children parameter. children: const [ // A SlidableAction can have an icon and/or a label. SlidableAction( backgroundColor: Color(0xFF21B7CA), foregroundColor: Colors.white, icon: Icons.edit, label: '編集', //エラー箇所↓ onPressed: () async { //指定した画面に遷移する await Navigator.of(context).push( MaterialPageRoute( builder: (context){ return edit_todo_page(); }, ), ); }, ), //エラー箇所↑ SlidableAction( backgroundColor: Color(0xFFFE4A49), foregroundColor: Colors.white, icon: Icons.delete, label: '削除', onPressed: null, ), ], ), ), ) .toList(); return ListView( children: listTiles, ); }, ), floatingActionButton: Consumer<TodoListModel>(builder: (context, model, child) { return FloatingActionButton( child: Icon(Icons.add), onPressed: () async { // todo await Navigator.push( context, MaterialPageRoute( builder: (context) => AddTodoPage(), fullscreenDialog: true, ), ); model.fetchTodos(); }, ); }), ), ); } }

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ざっとエラーに対しての内容とその修正方法を示してみました。(ここではエラーメッセージをgoogle翻訳したものを書いてます)
またedit_todo_page()だけは提示されたソースだけでは解決できません。

後表題の「ページの画面遷移が上手くいかない」に関しては、ソースコードに構文エラーがありプログラムが動かないので当然です。まずは構文エラー等を直してプログラムが起動するようにしてみてください。

  1. 引数の型 'void Function()' をパラメーターの型 'void Function(BuildContext)?' に割り当てることはできません。
    onPressedに設定できるのはvoid Function(BuildContext)。実装されているのはvoid Function()だからエラーになった。
  2. const リスト リテラルの値は定数でなければなりません。
    children : const[と定義しているけど中身に定義されているWidgetがconstにできないんでエラー。
    そのconstにできないとしている理由が、次のエラーとして表示されている場所の物。
  3. メソッド 'edit_todo_page' はタイプ 'TodoListPage' に対して定義されていません。
    importし忘れか、importしたソースに定義されていないのか、名前間違いかなにかか。

dart

1 children: [ // <= const 要らない 2 // A SlidableAction can have an icon and/or a label. 3 SlidableAction( 4 backgroundColor: Color(0xFF21B7CA), 5 foregroundColor: Colors.white, 6 icon: Icons.edit, 7 label: '編集', 8 onPressed: (context) async { // <= ()内に引数必要 9 //指定した画面に遷移する 10 await Navigator.of(context).push( 11 MaterialPageRoute( 12 builder: (context) { 13 return edit_todo_page(); // <= 未定義・これは本人にしか解決できないと思う。

投稿2022/09/11 11:36

ta.fu

総合スコア1667

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問