Flutter未定义名称“上下文”。尝试将名称更正为已定义的名称,或定义名称

时间:2020-07-12 12:30:10

标签: flutter dart

任何帮助,请问我如何在课堂上传递上下文 错误提示:未定义名称'context'。尝试将名称更正为已定义的名称,或定义名称。

当我将类别名称作为字符串传递时,它像下面的类一样很好用

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


}
    

1 个答案:

答案 0 :(得分: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, ...}更为优雅,这样您就可以知道调用构造函数时要传递的内容。