【问题标题】:error: The argument type 'Context' can't be assigned to the parameter type 'BuildContext'错误:参数类型“上下文”不能分配给参数类型“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

标签: flutter dart


【解决方案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();
  }
}

【讨论】:

    猜你喜欢
    • 2020-12-03
    • 2020-07-08
    • 2020-06-25
    • 1970-01-01
    • 1970-01-01
    • 2019-04-23
    • 1970-01-01
    • 2019-11-17
    相关资源
    最近更新 更多