flutter未定义的名称'上下文'用于本地化

发布于 2025-02-10 17:28:34 字数 4648 浏览 1 评论 0原文

有任何帮助,请如何通过此处的本地化上下文? 错误说:未定义的名称“上下文”。尝试将名称纠正为定义的名称,或定义名称。

category1.dart

import 'package:flutter/material.dart';
import 'package:myproject/classes/item.dart';
import 'page_details.dart';
import 'package:myproject/classes/language_constants.dart';

List<Item> detailsList = [
  Item(
      name: translation(context).name_1,
      image: 'assets/rivers/object1.jpg',
      details: translation(context).details_1,
      facts: [
        translation(context).fact_1_1,
        translation(context).fact_1_2,
        translation(context).fact_1_3,
      ],
    photolink: '',
    author: '',
    licensetype: '',
    licenselink: '',
  ),
  Item(
      name: translation(context).name_2,
      image: 'assets/rivers/object1.jpg',
      details: translation(context).details_2,
      facts: [
        translation(context).fact_2_1,
        translation(context).fact_2_2,
        translation(context).fact_2_3,
      ],
    photolink: '',
    author: '',
    licensetype: '',
    licenselink: '',
  ),
] 

tranlation()定义如下:

language_constants.dart

import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';

AppLocalizations translation(BuildContext context) {
  return AppLocalizations.of(context)!;
}

类项目定义如下:

item.dart

class Item {
  final String name;
  final String image;
  final String details;
  final String photolink;
  final String author;
  final String licensetype;
  final String licenselink;
  List<String> facts = <String>[];

  Item({
    required this.name,
    required this.image,
    required this.details,
    required this.facts,
    required this.photolink,
    required this.author,
    required this.licensetype,
    required this.licenselink,
  });
}

我将适当的类项目作为内容通过详细信息列表[index]

category1.dart

ElevatedButton.icon(
          style: ElevatedButton.styleFrom(
            padding: const EdgeInsets.only(left: 8, right: 8),
            minimumSize: const Size(30, 30),
          ),
          onPressed: () {
            _navigateToPageDetails(context, detailsList[index]);
          },
...
)

和材料pageroute():

category1.dart

_navigateToPageDetails(BuildContext context, Item item) async {
    await Navigator.push(
      context,
      MaterialPageRoute(
        builder: (context) => PageDetails(
          item: item,
        ),
      ),
    );
  }

最后:

page_details.dart

import 'package:myproject/classes/item.dart';

class PageDetails extends StatefulWidget {
  final Item item;

  const PageDetails({Key? key, required this.item}) : super(key: key);

  @override
  State<PageDetails> createState() => _PageDetailsState();
}

class _PageDetailsState extends State<PageDetails> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.item.name),
      ),
      body: SingleChildScrollView(
        child:Column(
          children: [
            Image.asset(widget.item.image),
            ...

所以,我不明白如何传递上下文对于类项目,这样简单的代码不会引发错误:

translation(context).name_1,
-->
Error: Undefined name 'context'.

我尝试将buildContext上下文上下文添加到类中,但这无济于事。 我尝试使用widget.context-相同,这无济于事。 也许班级项目应该扩展一些课程?但是尝试这也无济于事。

请帮助我..

在上面的Saichi Okuma的完美建议后,更新答案后

,我想从我的应用程序中分享代码,以便也许对其他人有用。

Item detailsList(BuildContext context, index) {
  final List<Item> list = [];

  list.addAll([
    Item(
        name: translation(context).name_1,
        image: 'assets/rivers/object1.jpg',
        details: translation(context).details_1,
        facts: [
          translation(context).fact_1_1,
          translation(context).fact_1_2,
          translation(context).fact_1_3,
        ],
      photolink: '',
      author: '',
      licensetype: '',
      licenselink: '',
    ),
    Item(
        name: translation(context).name_2,
        image: 'assets/rivers/object2.jpg',
        details: translation(context).details_2,
        facts: [
          translation(context).fact_2_1,
          translation(context).fact_2_2,
          translation(context).fact_2_3,
        ],
      photolink: '',
      author: '',
      licensetype: '',
      licenselink: '',
    ),
  ]);
  
  final Item element = list[index];

  return element;
}

上面的功能获取上下文并返回类项目所需的元素。

然后,我不使用列表(如旧的初始代码),而是将该方法称为如下:

onPressed: () {
            _navigateToPageDetails(context, detailsList(context, index));
          }, 

Any help please how do I pass the context for localization here?
error says: Undefined name 'context'. Try correcting the name to one that is defined, or defining the name.

category1.dart

import 'package:flutter/material.dart';
import 'package:myproject/classes/item.dart';
import 'page_details.dart';
import 'package:myproject/classes/language_constants.dart';

List<Item> detailsList = [
  Item(
      name: translation(context).name_1,
      image: 'assets/rivers/object1.jpg',
      details: translation(context).details_1,
      facts: [
        translation(context).fact_1_1,
        translation(context).fact_1_2,
        translation(context).fact_1_3,
      ],
    photolink: '',
    author: '',
    licensetype: '',
    licenselink: '',
  ),
  Item(
      name: translation(context).name_2,
      image: 'assets/rivers/object1.jpg',
      details: translation(context).details_2,
      facts: [
        translation(context).fact_2_1,
        translation(context).fact_2_2,
        translation(context).fact_2_3,
      ],
    photolink: '',
    author: '',
    licensetype: '',
    licenselink: '',
  ),
] 

tranlation() is defined as follows:

language_constants.dart

import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';

AppLocalizations translation(BuildContext context) {
  return AppLocalizations.of(context)!;
}

Class Item is defined as follows:

item.dart

class Item {
  final String name;
  final String image;
  final String details;
  final String photolink;
  final String author;
  final String licensetype;
  final String licenselink;
  List<String> facts = <String>[];

  Item({
    required this.name,
    required this.image,
    required this.details,
    required this.facts,
    required this.photolink,
    required this.author,
    required this.licensetype,
    required this.licenselink,
  });
}

I pass appropriate class Item as a content via detailsList[index]:

category1.dart

ElevatedButton.icon(
          style: ElevatedButton.styleFrom(
            padding: const EdgeInsets.only(left: 8, right: 8),
            minimumSize: const Size(30, 30),
          ),
          onPressed: () {
            _navigateToPageDetails(context, detailsList[index]);
          },
...
)

and MaterialPageRoute():

category1.dart

_navigateToPageDetails(BuildContext context, Item item) async {
    await Navigator.push(
      context,
      MaterialPageRoute(
        builder: (context) => PageDetails(
          item: item,
        ),
      ),
    );
  }

And finally the page:

page_details.dart

import 'package:myproject/classes/item.dart';

class PageDetails extends StatefulWidget {
  final Item item;

  const PageDetails({Key? key, required this.item}) : super(key: key);

  @override
  State<PageDetails> createState() => _PageDetailsState();
}

class _PageDetailsState extends State<PageDetails> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.item.name),
      ),
      body: SingleChildScrollView(
        child:Column(
          children: [
            Image.asset(widget.item.image),
            ...

So, I don't understand how to pass context to Class Item, so that this simple code doesn't throw the error:

translation(context).name_1,
-->
Error: Undefined name 'context'.

I've tried to add BuildContext context to the class, but it doesn't help.
I've tried to use widget.context - the same, it doesn't help.
Perhaps Class Item should extend some class? but experimenting with that also doesn't help.

Please help me..

Update after accepted answer

Following the perfect advice by Saichi Okuma above, I would like to share the code from my app, so that maybe it will be useful for somebody else.

Item detailsList(BuildContext context, index) {
  final List<Item> list = [];

  list.addAll([
    Item(
        name: translation(context).name_1,
        image: 'assets/rivers/object1.jpg',
        details: translation(context).details_1,
        facts: [
          translation(context).fact_1_1,
          translation(context).fact_1_2,
          translation(context).fact_1_3,
        ],
      photolink: '',
      author: '',
      licensetype: '',
      licenselink: '',
    ),
    Item(
        name: translation(context).name_2,
        image: 'assets/rivers/object2.jpg',
        details: translation(context).details_2,
        facts: [
          translation(context).fact_2_1,
          translation(context).fact_2_2,
          translation(context).fact_2_3,
        ],
      photolink: '',
      author: '',
      licensetype: '',
      licenselink: '',
    ),
  ]);
  
  final Item element = list[index];

  return element;
}

the function above gets the context and returns the required element of class Item.

and then instead of using a list (as in old initial code), I call the method as follows:

onPressed: () {
            _navigateToPageDetails(context, detailsList(context, index));
          }, 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无需解释 2025-02-17 17:28:34

当您构建列表时,没有上下文,这就是为什么您会遇到错误的原因。解决,保持逻辑的简单方法:


List<Item> detailsList(BuildContext context) {
  final List<Item> list = [];
  
  list.addAll([
    Item(
        name: translation(context).name_1,
        image: 'assets/rivers/object1.jpg',
        details: translation(context).details_1,
        facts: [
          translation(context).fact_1_1,
          translation(context).fact_1_2,
          translation(context).fact_1_3,
        ],
      photolink: '',
      author: '',
      licensetype: '',
      licenselink: '',
    ),
    Item(
        name: translation(context).name_2,
        image: 'assets/rivers/object1.jpg',
        details: translation(context).details_2,
        facts: [
          translation(context).fact_2_1,
          translation(context).fact_2_2,
          translation(context).fact_2_3,
        ],
      photolink: '',
      author: '',
      licensetype: '',
      licenselink: '',
    ),
  ]);
  
  return list;
} 

当您请求列表时,将上下文作为参数传递。

There is no context present when you build your list, that's why you get the error. A simple way to solve, keeping your logic:


List<Item> detailsList(BuildContext context) {
  final List<Item> list = [];
  
  list.addAll([
    Item(
        name: translation(context).name_1,
        image: 'assets/rivers/object1.jpg',
        details: translation(context).details_1,
        facts: [
          translation(context).fact_1_1,
          translation(context).fact_1_2,
          translation(context).fact_1_3,
        ],
      photolink: '',
      author: '',
      licensetype: '',
      licenselink: '',
    ),
    Item(
        name: translation(context).name_2,
        image: 'assets/rivers/object1.jpg',
        details: translation(context).details_2,
        facts: [
          translation(context).fact_2_1,
          translation(context).fact_2_2,
          translation(context).fact_2_3,
        ],
      photolink: '',
      author: '',
      licensetype: '',
      licenselink: '',
    ),
  ]);
  
  return list;
} 

When you request a list, pass the context as argument.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文