【问题标题】:Flutter Undefined name 'context'. Try correcting the name to one that is defined, or defining the name颤振未定义的名称“上下文”。尝试将名称更正为已定义的名称,或定义名称
【发布时间】:2020-11-01 18:39:37
【问题描述】:

任何帮助请我如何在这个类中传递上下文 错误说:未定义的名称“上下文”。尝试将名称更正为已定义的名称,或定义名称。

当我将类别名称作为字符串传递时,它工作正常,就像下面的类工作得很好

import 'package:flutter/material.dart';
import 'package:material_design_icons_flutter/material_design_icons_flutter.dart';
import 'package:domain/classes/localization.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';



// products categories
class Category  {
  int id;
  String name;
  Icon icon;
  Color color;

  Category(this.id, this.name, this.icon, this.color);
  static List<Category> getCategories() {

    return <Category>[
      Category(0, 'All' ,Icon(Icons.border_all, size: 30.0,), Colors.red),
      Category(1, 'Phones' ,Icon(Icons.phone_android, size: 30.0,), Colors.blue),
      Category(2, 'Electronics', Icon(Icons.devices_other, size: 30.0,), Colors.lightBlue),
      Category(3, 'Woman Fashions',Icon(MdiIcons.humanFemale, size: 30.0,), Colors.pink),
      Category(4, 'Woman Accessories',Icon(MdiIcons.shoeHeel, size: 30.0,), Colors.pink),
      Category(5, 'Man Fashions',Icon(MdiIcons.humanMale, size: 30.0,), Colors.brown),
      Category(6, 'Man Accessories',Icon(MdiIcons.shoeFormal, size: 30.0,), Colors.brown),
      Category(7, 'Baby Child', Icon(Icons.child_care, size: 30.0,), Colors.lightBlueAccent),
      Category(8, 'Sports Outdoors', Icon(Icons.fitness_center, size: 30.0,), Colors.blueAccent),
      Category(9, 'Gaming',Icon(Icons.videogame_asset, size: 30.0,), Colors.deepPurple),
      Category(10, 'Home Appliances', Icon(Icons.home, size: 30.0,), Colors.green),
      Category(11, 'Cars',Icon(Icons.directions_car, size: 30.0,), Colors.blueGrey),
      Category(12, 'Auto Parts', Icon(MdiIcons.carBattery, size: 30.0,), Colors.blueGrey),
      Category(13, 'Trucks Bus', Icon(Icons.local_shipping, size: 30.0,), Colors.blueGrey),
      Category(14, 'Other Vehicles', Icon(Icons.directions_boat, size: 30.0,), Colors.blueGrey),
      Category(15, 'Housing Sale', Icon(MdiIcons.homeGroup, size: 30.0,), Colors.black),
    ];
  }

但我想设置类别名称以根据用户设备语言自动翻译和加载, 所以当我从本地化类中调用翻译后的名称时,它会给我一个错误

Undefined name 'context'.  Try correcting the name to one that is defined, or defining the name.

我不知道如何传递上下文任何帮助

import 'package:flutter/material.dart';
import 'package:material_design_icons_flutter/material_design_icons_flutter.dart';
import 'package:domain/classes/localization.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';



// products categories
class Category  {
  int id;
  String name;
  Icon icon;
  Color color;

  Category(this.id, this.name, this.icon, this.color);
  static List<Category> getCategories() {

    return <Category>[
      Category(0, DemoLocalizations.of(context).allCategories ,Icon(Icons.border_all, size: 30.0,), Colors.red),
      Category(1, DemoLocalizations.of(context).phones ,Icon(Icons.phone_android, size: 30.0,), Colors.blue),
      Category(2, DemoLocalizations.of(context).electronics, Icon(Icons.devices_other, size: 30.0,), Colors.lightBlue),
    ];
  }


}
    

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    上下文不是全局变量。

    上下文只能在小部件内的build 方法、initState 等中访问。因此,您需要在创建类时将context 作为参数并将其分配给this.context

    class Category  {
      int id;
      String name;
      Icon icon;
      Color color;
      BuildContext context;
    
      Category(this.id, this.name, this.icon, this.color, this.context);
      static List<Category> getCategories() {
    
        return <Category>[
          Category(0, DemoLocalizations.of(context).allCategories ,Icon(Icons.border_all, size: 30.0,), Colors.red),
          Category(1, DemoLocalizations.of(context).phones ,Icon(Icons.phone_android, size: 30.0,), Colors.blue),
          Category(2, DemoLocalizations.of(context).electronics, Icon(Icons.devices_other, size: 30.0,), Colors.lightBlue),
        ];
      }
    
    }
    

    并且在调用时传递当前小部件的构建上下文Category(..., context);

    当有多个属性时,将它们全部设置为Category({@required this.id, @required this.name, ...} 会更优雅,这样您在调用构造函数时就会知道传递了什么。


    另请注意:

    context 传递给小部件是不好的做法。正如@GenchiGenbutsu 在 cmets 中所说的那样。

    使用状态管理代替InheritedWidget,或blocprovider等包。

    【讨论】:

    • 在模型中保存上下文不是一个聪明的想法和糟糕的风格
    • @GenchiGenbutsu 添加您的解决方案,我将删除我的解决方案。你会建议状态管理吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-28
    • 2020-09-03
    • 2023-02-07
    • 2022-08-22
    • 2021-03-24
    • 2022-06-10
    • 2021-11-28
    相关资源
    最近更新 更多