【问题标题】:same list is inputted but output comes out as blank输入了相同的列表,但输出为空白
【发布时间】:2019-09-09 07:59:08
【问题描述】:

我有一个 loginPage 飞镖使用列表来设置使用墨水井按钮的主页。我将列表移动到另一个 dart 文件并从那里导入列表。但是现在主页没有显示任何墨水井按钮。

我不记得我尝试了什么。

这是有效更改之前的代码。 https://pastebin.com/HMn5JUd4

这是非工作代码和列表。 https://pastebin.com/H2PfKFNp https://pastebin.com/684uzGQP

 void _loginPressed() {
    // these handlers are called whenever the user tries to login, resend password or create an account
    print('The use wants to login with $_email and $_password');
    //if (_email == ""&& _password == "") {
    Navigator.push(
        context,
        MaterialPageRoute(
            builder: (BuildContext context) =>
                BrowsePage(mLists.getMainButtonsList())));
    //}
  }

void _loginPressed() { // 每当用户尝试登录、重新发送密码或创建帐户时,都会调用这些处理程序 print('用户想用$_email和$_password登录'); //if (_email == ""&& _password == "") {

List<BuyItem> buyItemList = [
  BuyItem('Add a pack of 10 for \$2.99', 'assets/scantron.png'),
  BuyItem('Add a pack of 5 for \$1.99', 'assets/pens.png'),
  BuyItem('Add one for \$1.49', 'assets/notebook.png'),
...
    Icons.local_cafe,
    'Drinks',
    () {
      Navigator.of(context).push(Page(drinksList));
    },
  ),
];
Navigator.push(
    context,
    MaterialPageRoute(
        builder: (BuildContext context) => BrowsePage(buttonList)));
//}

}

The same void function in both pastebin code is being passed buttonList but the non-working code does not display a 2 x 2 grid of buttons.

【问题讨论】:

    标签: dart flutter


    【解决方案1】:

    您无需创建StatelessWidget 即可导出任何变量列表。

    删除这个空变量:

                  final List<BuyItem> buyItemList = new List();
    
                  final List<Product> suppliesList = new List();
                  final List<Product> foodList = new List();
                  final List<Product> toiletriesList = new List();
                  final List<Product> drinksList = new List();
    
                  final List<MainButtons> buttonList = new List();
    

    删除build 方法并提取这些变量:

        //remove this
                 @override
                  Widget build(BuildContext context) {
    

    更新 你的文件应该是这样的:

        class MyClass {
          final BuildContext context;
    
          MyClass(this.context);
    
          List<Product> suppliesList () => [
            Product('Scantrons', 'assets/scantron.png', () {
              Navigator.of(context).push(ItemsBuyPage([buyItemList[0]]));
            }),
            Product('Pens', 'assets/pens.png', () {
              Navigator.of(context).push(ItemsBuyPage([buyItemList[1]]));
            }),
            Product('Notebook', 'assets/notebook.png', () {
              Navigator.of(context).push(ItemsBuyPage([buyItemList[2]]));
            }),
            Product('Calculator', 'assets/calculator.png', () {
              Navigator.of(context).push(ItemsBuyPage([buyItemList[3]]));
            }),
          ];
    
           List<Product> get foodList => [
            Product('Cliff Bar', 'assets/cliff bar.png', () {
              Navigator.of(context).push(ItemsBuyPage([buyItemList[4]]));
            }),
            Product('Apple', 'assets/apple.png', () {
              Navigator.of(context).push(ItemsBuyPage([buyItemList[5]]));
            }),
            Product('Bannana', 'assets/bannana.png', () {
              Navigator.of(context).push(ItemsBuyPage([buyItemList[6]]));
            }),
            Product('Gum', 'assets/gum.png', () {
              Navigator.of(context).push(ItemsBuyPage([buyItemList[7]]));
            }),
          ];
    
           List<Product> get toiletriesList => [
            Product('Tissues', 'assets/tissues.png', () {
              Navigator.of(context).push(ItemsBuyPage([buyItemList[8]]));
            }),
            Product('Hand Sanitizer', 'assets/hand sanitizer.png', () {
              Navigator.of(context).push(ItemsBuyPage([buyItemList[9]]));
            }),
            Product('Toothbrush', 'assets/toothbrush.png', () {
              Navigator.of(context).push(ItemsBuyPage([buyItemList[10]]));
            }),
            Product('Deoderant', 'assets/deoderant.png', () {
              Navigator.of(context).push(ItemsBuyPage([buyItemList[11]]));
            }),
          ];
    
           List<Product> get drinksList => [
            Product('Energy Drink', 'assets/energy drink.png', () {
              Navigator.of(context).push(ItemsBuyPage([buyItemList[12]]));
            }),
            Product('Water Bottle', 'assets/water bottle.png', () {
              Navigator.of(context).push(ItemsBuyPage([buyItemList[13]]));
            }),
            Product('Gatorade', 'assets/gatorade.png', () {
              Navigator.of(context).push(ItemsBuyPage([buyItemList[14]]));
            }),
            Product('Coffee', 'assets/coffee.png', () {
              Navigator.of(context).push(ItemsBuyPage([buyItemList[15]]));
            }),
          ];
    
           List<MainButtons> get buttonList => [
            MainButtons(
              Icons.domain,
              'Class Supplies',
                  () {
                Navigator.of(context).push(Page(suppliesList));
              },
            ),
            MainButtons(
              Icons.local_dining,
              'Food and Snacks',
                  () {
                Navigator.of(context).push(Page(foodList));
              },
            ),
            MainButtons(
              Icons.hot_tub,
              'Personal Supplies',
                  () {
                Navigator.of(context).push(Page(toiletriesList));
              },
            ),
            MainButtons(
              Icons.local_cafe,
              'Drinks',
                  () {
                Navigator.of(context).push(Page(drinksList));
              },
            ),
          ];
        }
    
          List<MainButtons> getMainButtonsList() {
            return buttonList;
          }
        }
    

    用法

                BrowsePage(MyClass(context).getMainButtonsList())
    

    【讨论】:

    • 我遇到了一些错误,我通过重新排列函数和更改一些变量来解决这些错误。我将如何修复上下文未定义错误?
    • 移除这个变量 final ButtonsLists mLists = new ButtonsLists();在你的 _LoginPageState 类中
    • 好的,我知道一切是如何工作的,但是如果 Widget build(BuildContext context) { 消失了,上下文未定义,要点应该与您的答案相匹配,我该怎么处理上下文
    • 好的,使用新列表“context、buyItemList[x] 和其他列表洗浴用品列表等”。不是静态成员,因此无法在初始化程序中访问,buttonList 未定义,需要一个 getter 方法、setter 或操作声明。您想将此对话用于聊天/不和谐吗?我有其他飞镖使用你不知道的列表,所以我添加了其中一个飞镖
    • 我很困惑,这是什么意思,gist 代码与我的 Android Studio 代码相同,除了 gist 不包含所有 AS 飞镖
    猜你喜欢
    • 1970-01-01
    • 2011-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-09
    • 1970-01-01
    • 2021-10-09
    • 2022-10-25
    相关资源
    最近更新 更多