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

时间:2021-05-02 10:34:24

标签: flutter dart parameter-passing flutter-widget flutter-state

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

我尝试使用“widget.username”方法从 _HomePageState 访问用户名参数。但它给出了一个错误,说“实例成员 'widget' 无法在初始化程序中访问”

以下是HomePage类的代码供大家参考:

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

问题出在 widgetsToDisplay 列表中,当 Profile 类被调用时,为 username 属性传递了 widget.username。

我用注释标记了给出错误的行。

感谢任何帮助。谢谢。

2 个答案:

答案 0 :(得分:0)

您的编译器是正确的,您无法在初始化程序中访问该变量,因此不要在不必要时将其设为初始化程序。

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

答案 1 :(得分:0)

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

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;

现在您可以在构建方法中使用 _username。