错误:参数类型“上下文”不能分配给参数类型“BuildContext”

Posted

技术标签:

【中文标题】错误:参数类型“上下文”不能分配给参数类型“BuildContext”【英文标题】:error: The argument type 'Context' can't be assigned to the parameter type 'BuildContext' 【发布时间】:2020-04-01 05:36:50 【问题描述】:

此编译错误:错误:无法将参数类型“Context”分配给参数类型“BuildContext”(argument_type_not_assignable at [tter] lib\Pages\list_view.dart:95)

不知道为什么突然出现这个错误 该应用程序运行良好,但我的代码中仍然出现错误 这是我的代码:

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();
  

【问题讨论】:

你能评论一下错误在哪里吗?此外,您可以使用widget.whereComeFrom 来获取值,您不需要通过构造函数传递它。 此处显示对话框下的错误:context : context 【参考方案1】:

State 提供的上下文不是最佳选择。我建议将 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();
  

【讨论】:

以上是关于错误:参数类型“上下文”不能分配给参数类型“BuildContext”的主要内容,如果未能解决你的问题,请参考以下文章

获取错误“未知”类型的参数不能分配给“错误”类型的参数 |空值'

颤振列表错误参数类型'List'不能分配给参数类型'String'

Flutter - 错误:参数类型'String/*1*/'不能分配给参数类型'String/*2*/'

错误:参数类型 'void Function()?'不能分配给参数类型 'void Function(String?)?'

错误:参数类型“对象?”不能分配给参数类型“字符串”

错误:不能将参数类型“String”分配给参数类型“Uri”。使用JsonPlaceHolder