Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 将变量从另一个类的状态传递给另一个类_Flutter_Dart_Parameter Passing_Flutter Widget_Flutter State - Fatal编程技术网

Flutter 将变量从另一个类的状态传递给另一个类

Flutter 将变量从另一个类的状态传递给另一个类,flutter,dart,parameter-passing,flutter-widget,flutter-state,Flutter,Dart,Parameter Passing,Flutter Widget,Flutter State,我试图将从另一个类接收到的参数username传递给我的HomePage类。 我想将该参数从HomePageState传递到另一个名为Profile的类 我尝试使用“widget.username”方法从_HomePageState访问username参数。但它给出了一个错误,即“无法在初始值设定项中访问实例成员‘widget’” 以下是主页类的代码,供您参考: import 'package:flutter/cupertino.dart'; import 'package:flutter/ma

我试图将从另一个类接收到的参数username传递给我的HomePage类。 我想将该参数从HomePageState传递到另一个名为Profile的类

我尝试使用“widget.username”方法从_HomePageState访问username参数。但它给出了一个错误,即“无法在初始值设定项中访问实例成员‘widget’”

以下是主页类的代码,供您参考:

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:codeforces_data_flutter/screens/profile.dart';

class HomePage extends StatefulWidget {
  String username;
  HomePage({this.username});

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

class _HomePageState extends State<HomePage> {
  static const widgetStyle =
      TextStyle(fontWeight: FontWeight.bold, fontSize: 30.0);

  int selectedIndex = 0;
  List<Widget> widgetsToDisplay = [
    Profile(
      username: widget.username,             /*Error is on this line*/
    ),
    Center(
      child: Text(
        'Index 1 : Previous Contests',
        style: widgetStyle,
      ),
    ),
    Center(
      child: Text(
        'Index 2 : Upcoming Contests',
        style: widgetStyle,
      ),
    ),
    Center(
      child: Text(
        'Index 3 : About Me',
        style: widgetStyle,
      ),
    ),
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      bottomNavigationBar: BottomNavigationBar(
        items: <BottomNavigationBarItem>[
          BottomNavigationBarItem(
            icon: Icon(
              CupertinoIcons.profile_circled,
            ),
            label: 'User Profile',
            backgroundColor: Colors.teal,
          ),
          BottomNavigationBarItem(
            icon: Icon(
              CupertinoIcons.arrow_counterclockwise,
            ),
            label: 'Previous Contests',
            backgroundColor: Colors.blue,
          ),
          BottomNavigationBarItem(
            icon: Icon(
              CupertinoIcons.arrow_clockwise,
            ),
            label: 'Upcoming Contests',
            backgroundColor: Colors.green,
          ),
          BottomNavigationBarItem(
            icon: Icon(
              CupertinoIcons.suit_heart,
            ),
            label: 'About Me',
            backgroundColor: Colors.pink,
          ),
        ],
        currentIndex: selectedIndex,
        selectedItemColor: Colors.yellow,
        onTap: (index) {
          setState(() {
            selectedIndex = index;
          });
        },
      ),
      body: widgetsToDisplay[selectedIndex],
    );
  }
}
import'包装:flift/cupertino.dart';
进口“包装:颤振/材料.省道”;
导入“package:codeforces_data_flatter/screens/profile.dart”;
类主页扩展了StatefulWidget{
字符串用户名;
主页({this.username});
@凌驾
_HomePageState createState()=>\u HomePageState();
}
类_HomePageState扩展状态{
静态常量widgetStyle=
TextStyle(fontWeight:fontWeight.bold,fontSize:30.0);
int selectedIndex=0;
列表widgetsToDisplay=[
侧面图(
用户名:widget.username,/*错误在此行*/
),
居中(
子:文本(
“索引1:以前的比赛”,
风格:widgetStyle,
),
),
居中(
子:文本(
“索引2:即将举行的比赛”,
风格:widgetStyle,
),
),
居中(
子:文本(
"索引3:关于我",,
风格:widgetStyle,
),
),
];
@凌驾
小部件构建(构建上下文){
返回脚手架(
底部导航栏:底部导航栏(
项目:[
底部导航气压计(
图标:图标(
CupertinoIcons.profile_已圈出,
),
标签:“用户配置文件”,
背景颜色:Colors.teal,
),
底部导航气压计(
图标:图标(
CupertinoIcons.逆时针方向的箭头,
),
标签:“以前的比赛”,
背景颜色:Colors.blue,
),
底部导航气压计(
图标:图标(
CupertinoIcons.arrow_顺时针,
),
标签:“即将到来的比赛”,
背景颜色:Colors.green,
),
底部导航气压计(
图标:图标(
CupertinoIcons.suit_heart,
),
标签:“关于我”,
背景颜色:Colors.pink,
),
],
currentIndex:selectedIndex,
选择编辑颜色:Colors.yellow,
onTap:(索引){
设置状态(){
selectedIndex=索引;
});
},
),
正文:widgetsToDisplay[selectedIndex],
);
}
}
当通过传递username属性的widget.username调用Profile类时,问题在于widgetsToDisplay列表

我已经使用注释标记了给出错误的行


感谢您的帮助。谢谢。

您的编译器是正确的,您无法在初始值设定项中访问该变量,因此在不需要时不要将其设置为初始值设定项


将小部件构建代码(在本例中为列表)移动到构建函数中,或移动到一个单独的函数中,然后在构建函数中调用该函数。

我不久前也遇到过同样的问题。试试这个:

class HomePage extends StatefulWidget {
final username;
HomePage({this.username});

  @override
  _ HomePage State createState() => _ HomePage State(this.username);
}

class _HomePageState extends State<HomePage> {
  _HomePageState(String username) {
    this._username = username;
  }
  String _username;
类主页扩展StatefulWidget{
最终用户名;
主页({this.username});
@凌驾
_主页状态createState()=>uu主页状态(this.username);
}
类_HomePageState扩展状态{
_HomePageState(字符串用户名){
这个.\u username=用户名;
}
字符串\u用户名;

现在你可以在你的构建方法中使用_username了。

它工作得很好,谢谢。但是你能解释一下为什么它以前不工作,以及在构建方法中添加列表是如何工作的吗?谢谢Raphael的回答。实际上,我在构建方法之外调用了Profile类,并且面临着这个问题,因为这很有帮助。当我在构建方法中移动列表时,错误就消失了。