颤振:未定义的名称 'context'
Flutter: Undefined name 'context'
我目前正在编写一个应用程序,我在页面路由方面遇到了一些问题。我使用的是 RouteGenerator
,除了在这种情况下,它工作得很好。
如果我尝试导航到我的第二个屏幕,我会收到一条错误消息:
Undefined name 'context'. Try correcting the name to one that is
defined, or defining the name.
如果有人能帮助我,我将不胜感激。
这是我的代码(项目 Class 在不同的文件中):
import 'package:flutter/material.dart';
class ProjectCard extends StatefulWidget {
const ProjectCard({
Key? key,
required this.project,
}) : super(key: key);
final Project project;
@override
_ProjectCardState createState() => _ProjectCardState();
}
class _ProjectCardState extends State<ProjectCard> {
// ignore: non_constant_identifier_names
List<Project> demo_projects = [];
@override
void initState() {
super.initState();
demo_projects.add(Project(title: 'Test', description: 'Test', onTap: () {Navigator.pushNamed(context, '/second');}),);
}
@override
Widget build(BuildContext context) {
return InkWell(
onTap: () {},
child: Container(
decoration: BoxDecoration(
color: secondaryColor,
borderRadius: BorderRadius.all(Radius.circular(30))),
padding: const EdgeInsets.all(defaultPadding),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
widget.project.title!,
maxLines: 2,
overflow: TextOverflow.ellipsis,
style: TextStyle(fontSize: 20, color: primaryColor),
),
Spacer(),
Text(
widget.project.description!,
maxLines: Responsive.isMobileLarge(context) ? 3 : 4,
overflow: TextOverflow.ellipsis,
style: TextStyle(height: 1.5),
),
Spacer(),
],
)),
);
}
}
import 'package:flutter/material.dart';
class ProjectsGridView extends StatelessWidget {
const ProjectsGridView({
Key? key,
this.crossAxisCount = 3,
this.childAspectRatio = 1.5,
}) : super(key: key);
final int crossAxisCount;
final double childAspectRatio;
@override
Widget build(BuildContext context) {
return GridView.builder(
shrinkWrap: true,
physics: NeverScrollableScrollPhysics(),
itemCount: demo_projects.length,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: crossAxisCount,
childAspectRatio: childAspectRatio,
crossAxisSpacing: defaultPadding,
mainAxisSpacing: defaultPadding,
),
itemBuilder: (context, index) => ProjectCard(
project: demo_projects[index],
),
);
}
}
class Project {
final String? title, description;
void Function () onTap;
Project({this.title, this.description, required this.onTap});
}
// ignore: non_constant_identifier_names
List<Project> demo_projects = [
Project(
title: "Test",
description:
"Test",
onTap: (){},
)
];
您收到此错误是因为您没有在任何地方定义变量上下文并试图传递它。
List<Project> demo_projects = [
Project(
title: "Test",
description:
"Test",
onTap: (){Navigator.pushNamed(**context**, '/second');},
),
通常,您在小部件的构建方法中有一个上下文。
@override
Widget build(BuildContext context) {
}
您没有提供完整的代码片段,所以我不知道完整的实现。
尝试声明上下文变量希望对您有所帮助。
class Project {
final BuildContext context;
final String title, description;
void Function() onTap;
Project({
this.title,
this.context,
this.description,
@required this.onTap,
});
}
BuildContext context;
List<Project> demo_projects = [
Project(
context: context,
title: "Test",
description: "Test",
onTap: () {
Navigator.pushNamed(context, '/second');
},
),
];
让你的 class 有状态并尝试使用 initstate
import 'package:flutter/material.dart';
class ProjectCard extends StatefulWidget {
const ProjectCard({
Key? key,
required this.project,
}) : super(key: key);
final Project project;
@override
_ProjectCardState createState() => _ProjectCardState();
}
class _ProjectCardState extends State<ProjectCard> {
List<Project> demo_projects= [];
@override
void initState() {
// TODO: implement initState
super.initState();
demo_projects.add(Project(
title: "Test",
description:
"Test",
onTap: (){Navigator.pushNamed(context, '/second');},
),);
}
@override
Widget build(BuildContext context) {
return InkWell(
onTap: () {},
child: Container(
decoration: BoxDecoration(
color: secondaryColor,
borderRadius: BorderRadius.all(Radius.circular(30))),
padding: const EdgeInsets.all(defaultPadding),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
widget.project.title!,
maxLines: 2,
overflow: TextOverflow.ellipsis,
style: TextStyle(fontSize: 20, color: primaryColor),
),
Spacer(),
Text(
widget.project.description!,
maxLines: Responsive.isMobileLarge(context) ? 3 : 4,
overflow: TextOverflow.ellipsis,
style: TextStyle(height: 1.5),
),
Spacer(),
],
)),
);
}
}
我目前正在编写一个应用程序,我在页面路由方面遇到了一些问题。我使用的是 RouteGenerator
,除了在这种情况下,它工作得很好。
如果我尝试导航到我的第二个屏幕,我会收到一条错误消息:
Undefined name 'context'. Try correcting the name to one that is defined, or defining the name.
如果有人能帮助我,我将不胜感激。
这是我的代码(项目 Class 在不同的文件中):
import 'package:flutter/material.dart';
class ProjectCard extends StatefulWidget {
const ProjectCard({
Key? key,
required this.project,
}) : super(key: key);
final Project project;
@override
_ProjectCardState createState() => _ProjectCardState();
}
class _ProjectCardState extends State<ProjectCard> {
// ignore: non_constant_identifier_names
List<Project> demo_projects = [];
@override
void initState() {
super.initState();
demo_projects.add(Project(title: 'Test', description: 'Test', onTap: () {Navigator.pushNamed(context, '/second');}),);
}
@override
Widget build(BuildContext context) {
return InkWell(
onTap: () {},
child: Container(
decoration: BoxDecoration(
color: secondaryColor,
borderRadius: BorderRadius.all(Radius.circular(30))),
padding: const EdgeInsets.all(defaultPadding),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
widget.project.title!,
maxLines: 2,
overflow: TextOverflow.ellipsis,
style: TextStyle(fontSize: 20, color: primaryColor),
),
Spacer(),
Text(
widget.project.description!,
maxLines: Responsive.isMobileLarge(context) ? 3 : 4,
overflow: TextOverflow.ellipsis,
style: TextStyle(height: 1.5),
),
Spacer(),
],
)),
);
}
}
import 'package:flutter/material.dart';
class ProjectsGridView extends StatelessWidget {
const ProjectsGridView({
Key? key,
this.crossAxisCount = 3,
this.childAspectRatio = 1.5,
}) : super(key: key);
final int crossAxisCount;
final double childAspectRatio;
@override
Widget build(BuildContext context) {
return GridView.builder(
shrinkWrap: true,
physics: NeverScrollableScrollPhysics(),
itemCount: demo_projects.length,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: crossAxisCount,
childAspectRatio: childAspectRatio,
crossAxisSpacing: defaultPadding,
mainAxisSpacing: defaultPadding,
),
itemBuilder: (context, index) => ProjectCard(
project: demo_projects[index],
),
);
}
}
class Project {
final String? title, description;
void Function () onTap;
Project({this.title, this.description, required this.onTap});
}
// ignore: non_constant_identifier_names
List<Project> demo_projects = [
Project(
title: "Test",
description:
"Test",
onTap: (){},
)
];
您收到此错误是因为您没有在任何地方定义变量上下文并试图传递它。
List<Project> demo_projects = [
Project(
title: "Test",
description:
"Test",
onTap: (){Navigator.pushNamed(**context**, '/second');},
),
通常,您在小部件的构建方法中有一个上下文。
@override
Widget build(BuildContext context) {
}
您没有提供完整的代码片段,所以我不知道完整的实现。
尝试声明上下文变量希望对您有所帮助。
class Project {
final BuildContext context;
final String title, description;
void Function() onTap;
Project({
this.title,
this.context,
this.description,
@required this.onTap,
});
}
BuildContext context;
List<Project> demo_projects = [
Project(
context: context,
title: "Test",
description: "Test",
onTap: () {
Navigator.pushNamed(context, '/second');
},
),
];
让你的 class 有状态并尝试使用 initstate
import 'package:flutter/material.dart';
class ProjectCard extends StatefulWidget {
const ProjectCard({
Key? key,
required this.project,
}) : super(key: key);
final Project project;
@override
_ProjectCardState createState() => _ProjectCardState();
}
class _ProjectCardState extends State<ProjectCard> {
List<Project> demo_projects= [];
@override
void initState() {
// TODO: implement initState
super.initState();
demo_projects.add(Project(
title: "Test",
description:
"Test",
onTap: (){Navigator.pushNamed(context, '/second');},
),);
}
@override
Widget build(BuildContext context) {
return InkWell(
onTap: () {},
child: Container(
decoration: BoxDecoration(
color: secondaryColor,
borderRadius: BorderRadius.all(Radius.circular(30))),
padding: const EdgeInsets.all(defaultPadding),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
widget.project.title!,
maxLines: 2,
overflow: TextOverflow.ellipsis,
style: TextStyle(fontSize: 20, color: primaryColor),
),
Spacer(),
Text(
widget.project.description!,
maxLines: Responsive.isMobileLarge(context) ? 3 : 4,
overflow: TextOverflow.ellipsis,
style: TextStyle(height: 1.5),
),
Spacer(),
],
)),
);
}
}