Flutter:失败的断言:'context!= null':不正确

问题描述 投票:0回答:1

[在Google上搜索了一段时间后,我非常想寻求您的帮助,以解决我自己无法解决的问题。不久,我调用一个警报对话框,并收到以下错误:'package:flutter / src / widgets / localizations.dart':断言失败:第446行pos 12:'context!=null':不正确。

这是我的代码的开头:我从中调用警报的第一个文件(类):'''

import 'package:flutter/material.dart';
import 'dart:async';
import 'package:path/path.dart';
import 'package:testspourprojets/outils/alert_connexion.dart';
import 'package:testspourprojets/outils/test_fichier.dart';
import 'package:testspourprojets/pages/page_detail_commerce.dart';
import 'package:testspourprojets/outils/variables_globales.dart';
class PageCommerces extends StatefulWidget {
_PageCommerces createState() => new _PageCommerces();
}
class _PageCommerces extends State<PageCommerces> {
TextEditingController editingController = TextEditingController();
List<Commercant>();
List<Commercant> dummySearchList = List<Commercant>();
List<Commercant> dummyListData = List<Commercant>();
@override
void initState() {
// TODO: implement initState
super.initState();
typePost = "commercant";
maLocalisation();
}
@override
Widget build(BuildContext context) {
// TODO: implement build
return new Scaffold(
appBar: new AppBar(
title: new Text("Commerçants"),
leading: new Container(), //cache le bouton back dans la appBar
),
body: new Container(
color: Theme.of(context).primaryColor,
child: (maListeCommerces == null)? chargementEnCours(): listeRetour(),
),// fin container
);
}
Widget listeRetour(){
fonctionConversion();
triSurDistance();
BuildContext context;
message1 = "Test alerte";
message2 = "Message de test d'alerte";
print("context hashcode : $context.hashCode");
AlerteConnexion().methodeAlerte();//c'est ici que j'appelle mon alerte dans une autre classe
return new Container(
...

'''除此之外,我在另一个文件中有AlerteConnexion类:'''

import 'package:flutter/material.dart';
import 'package:testspourprojets/pages/page_promotions.dart';
import 'package:testspourprojets/outils/variables_globales.dart';
class AlerteConnexion extends StatefulWidget {
_AlerteConnexion createState() => new _AlerteConnexion();
methodeAlerte() => createState().Alerte();
}
class _AlerteConnexion extends State<AlerteConnexion>{
@override
Widget build(BuildContext context) {
// TODO: implement build
print("Dans AlerteConnexion");
return null;
}
void Alerte(){
//build(context);
print("Dans Alerte");
showDialog(
context: context,
barrierDismissible: true,
builder: (BuildContext contextAlert) {
return new AlertDialog(
title: new Text(message1, textScaleFactor: 1.5,),
content: new Text(message2),
contentPadding: EdgeInsets.all(5.0),
actions: <Widget>[
new FlatButton(
onPressed: () {
print("Continuer");
Navigator.pop(contextAlert);
},
child: new Text(
"Continuer",
style: new TextStyle(color: Colors.deepPurple),))
],
);
},
);
}
}

'''

请注意,如果我将警报放置在第一个文件/类中,则它也不起作用。我不是一个专业的开发人员,我必须承认我在掌握(理解)状态的概念以及树中小部件的位置时遇到了一些困难(我什至不确定函数是否是小部件)。非常感谢您的帮助!祝您有个美好的夜晚/一天伯纳德

flutter
1个回答
0
投票

[有人帮了我,我必须在我的函数(listeRetour)中将上下文作为参数传递。我的上下文不再为null,但是我收到另一条错误消息,提示它试图同时构建两个上下文(如果我理解得很好)。请注意,其余代码中没有setState。

The following assertion was thrown building PageCommerces(dirty, dependencies: [_InheritedTheme,
flutter: _LocalizationsScope-[GlobalKey#eee58]], state: _PageCommerces#6633b):
flutter: setState() or markNeedsBuild() called during build.
flutter: This Overlay widget cannot be marked as needing to build because the framework is already in the
flutter: process of building widgets. A widget can be marked as needing to be built during the build phase
flutter: only if one of its ancestors is currently building. This exception is allowed because the framework
flutter: builds parent widgets before children, which means a dirty descendant will always be built.
flutter: Otherwise, the framework might not visit this widget during this build phase.
flutter: The widget on which setState() or markNeedsBuild() was called was:
flutter: Overlay-[LabeledGlobalKey<OverlayState>#fd124](state: OverlayState#a69a2(entries:
flutter: [OverlayEntry#f57bb(opaque: false; maintainState: false), OverlayEntry#728b4(opaque: false;
flutter: maintainState: true), OverlayEntry#4d6c6(opaque: false; maintainState: false),
flutter: OverlayEntry#bb803(opaque: false; maintainState: true)]))
flutter: The widget which was currently being built when the offending call was made was:
flutter: PageCommerces(dirty, dependencies: [_InheritedTheme, _LocalizationsScope-[GlobalKey#eee58]],
flutter: state: _PageCommerces#6633b) 

感谢您的帮助!

© www.soinside.com 2019 - 2024. All rights reserved.