前提
ログインをしてログイン後はホームとアカウントの2画面があります。
BottomNavigationBarを使用して画面の切り替えを行います。
ログイン時にログイン情報を取得し、アカウント画面でログイン情報を表示させたいと思ってます。
実現したいこと
・他のファイルのクラスに値を渡したして渡したデータを画面上に表示させたい。
※現在お試しでtest2のみを送ろうとしておりますが実際はほかのデータも送る予定です。
発生している問題・エラーメッセージ
①Error: Can't access 'this' in a field initializer to read 'test2'. Account(user_id2: test2), ②Error: Undefined name 'test2'. Account(user_id2: test2), ※①が解消されれば②も解消されるはず。
該当のソースコード
Dart
1import 'package:flutter/material.dart'; 2import 'account.dart'; 3import 'home.dart'; 4// [Themelist] インスタンスにおける処理。 5class Appmain extends StatelessWidget{ 6 final String? user_id;//null許容で変数定義 7 final String? imgURL;//null許容で変数定義 8 final String? user_email;//null許容で変数定義 9 final String? user_name;//null許容で変数定義 10 const Appmain({Key? key, this.user_id, this.imgURL, this.user_email, this.user_name}) : super(key: key); 11 12 Widget build(BuildContext context) { 13 print(user_name); 14 return Scaffold ( 15 body: MyTop(test: user_name), 16 ); 17 } 18} 19 20// ignore: must_be_immutable, use_key_in_widget_constructors 21class MyTop extends StatefulWidget{ 22 final String? test; 23 const MyTop({Key? key, this.test}) : super(key: key); 24 25 // ignore: no_logic_in_create_state 26 _MyTop2 createState() => _MyTop2(test2: test); 27} 28class _MyTop2 extends State<MyTop> { 29 final String? test2; 30 _MyTop2({this.test2}); 31 static const _screens = [ 32 Home(), 33 Account(user_id2: test2), 34 ]; 35 int _selectedIndex = 0; 36 37 void _onItemTapped(int index) { 38 setState(() { 39 _selectedIndex = index; 40 }); 41 } 42 43 Widget build(BuildContext context) { 44 print(test2); 45 return Scaffold ( 46 body: _screens[_selectedIndex], 47 bottomNavigationBar: BottomNavigationBar( 48 currentIndex: _selectedIndex, 49 onTap: _onItemTapped, 50 items: const <BottomNavigationBarItem>[ 51 BottomNavigationBarItem(icon: Icon(Icons.home), label: 'ホーム'), 52 BottomNavigationBarItem(icon: Icon(Icons.person), label: 'アカウント'), 53 ], 54 type: BottomNavigationBarType.fixed, 55 ) 56 ); 57 } 58}
試したこと
・printにて値を表示してみたら表示はされたので変数自体に値は入っていそう。
・Account(user_id2: test2),のところで変数のtest2ではなく任意の文字列を入れるとエラーはなくなり他のファイルにも値を渡せた。
補足情報(FW/ツールのバージョンなど)
OS:Windows11
IDE:Android Studio
Flutter 3.0.4
firebase_auth: ^3.6.3
firebase_core: ^1.20.1
cloud_firestore: ^3.4.4
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/08/25 09:42
2022/08/25 13:34