Zeone line
Zeone line

Reputation: 339

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

Any help please how do i pass the context in this class error says: Undefined name 'context'. Try correcting the name to one that is defined, or defining the name.

when i pass the categories name as a string it works fine like the class below works great

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

But i want to set the names of categories to be automatically translated and loaded based on user device language, so when i call for the translated name from the localization class it gives me an error

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

i dont know how to pass the context any help please

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


}
    

Upvotes: 4

Views: 7031

Answers (1)

Phani Rithvij
Phani Rithvij

Reputation: 4507

Context is not a global variable.

Context can only be accessed in a build method, initState etc. inside a widget. So you need to take the context as an argument when creating your class and assign it to 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),
    ];
  }

}

And when calling pass the current widget's build context Category(..., context);

And when there are multiple properties it's more elegant to have them all as Category({@required this.id, @required this.name, ...} So that you'll know what you're passing when calling the constructor.


Also Note:

Passing context to widgets is bad practice. As @GenchiGenbutsu said in the comments.

Use state management instead like InheritedWidget, or packages like bloc, provider.

Upvotes: 3

Related Questions