首页 > 解决方案 > 断言失败:'context != null':不正确

问题描述

在谷歌上搜索了一段时间后,我拼命请求您的帮助,以解决我自己无法解决的问题。很快,我调用了一个警告对话框,我收到以下错误:'package:flutter/src/widgets/localizations.dart': Failed assertion: line 446 pos 12: 'context != null': is not true。

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

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

'''

请注意,如果我将警报放在第一个文件/类中,它也不起作用。我不是专业的开发人员,我必须承认我在掌握(理解)State 的概念和 Widget 在树中的位置方面有些困难(我什至不确定一个函数是否是一个小部件)。我非常感谢您的帮助!祝你晚上/白天愉快,伯纳德

标签: flutter

解决方案


推荐阅读