简体   繁体   中英

error: The argument type 'Context' can't be assigned to the parameter type 'BuildContext'

this compile error : error: The argument type 'Context' can't be assigned to the parameter type 'BuildContext'(argument_type_not_assignable at [tter] lib\\Pages\\list_view.dart:95)

I don't know why this error is showed suddenly the app is work fine but I still see error in my code
this my code :

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

The Context provided by State isn't the best option. I recommend passing a BuildContext to the _showDialog() method. I've posted the code below to show you. I've also made it so that you don't need a constructor in your State class.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM