此编译错误:错误:无法将参数类型“上下文”分配给参数类型“ BuildContext”([tter] lib \ Pages \ list_view.dart:95处的argument_type_not_assignable)
我不知道为什么突然显示此错误
该应用程序正常运行,但我的代码中仍然出现错误
这是我的代码:
import 'package:path/path.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:tter/utilities/database_helper.dart';
TextEditingController searchText = TextEditingController();
class CardsListView extends StatefulWidget {
int whereComeFrom;
CardsListView(this.whereComeFrom);
@override
CardsListViewState createState() => CardsListViewState(whereComeFrom);
}
class CardsListViewState extends State<CardsListView> {
int whereComeFrom;
CardsListViewState(this.whereComeFrom);
var db = DatabaseHelper();
List mainList = [];
_showDialog() {
showDialog(
context: context,
builder: (BuildContext context){
return StatefulBuilder(
builder: (context,sett){
void _showSearchReturn(String query) async{
}
return Container(
);
},
);
}
);
}
@override
Widget build(BuildContext context){
return Container();
}
}
答案 0 :(得分:1)
国家提供的上下文不是最佳选择。我建议将BuildContext传递给_showDialog()
方法。我已将以下代码发布给您。我也这样做了,因此您在State类中不需要构造函数。
import 'package:path/path.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:tter/utilities/database_helper.dart';
TextEditingController searchText = TextEditingController();
class CardsListView extends StatefulWidget {
final int whereComeFrom; // immutable class, should be declared as final for all variables
CardsListView(this.whereComeFrom);
@override
CardsListViewState createState() => CardsListViewState();
}
class CardsListViewState extends State<CardsListView> {
// IMPORTANT! You can use widget.whereComeFrom to get the value.
// You DON'T need to pass a variable to the state.
var db = DatabaseHelper();
List mainList = [];
// Pass an actual BuildContext here.
// The context given from State isn't the best option.
// If you call this from a Builder, just pass the context.
_showDialog(BuildContext context) {
showDialog(
context: context,
builder: (BuildContext context){
return StatefulBuilder(
builder: (context,sett){
void _showSearchReturn(String query) async{
}
return Container(
);
},
);
}
);
}
@override
Widget build(BuildContext context){
return Container();
}
}