Flutter错误:没有名为'child'的参数

4

我需要帮助,当我在Android Studio中运行下面的代码时,出现以下错误:

我还在下面发布了由Android Studio生成的错误消息。

我运行了flutter升级命令,现在我收到一个错误消息,说“child”这个命名参数没有定义。该项目是新创建的,且默认代码未更改,但仍然出现相同的错误:

import 'package:app_markeeting_and_promotion/models/home_model.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:device_apps/device_apps.dart';
import 'package:firebase_admob/firebase_admob.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class DownlaodsApp extends StatefulWidget {
  @override
  _DownlaodsAppState createState() => _DownlaodsAppState();
}

class _DownlaodsAppState extends State<DownlaodsApp> {
  var stream = FirebaseFirestore.instance;
  static const MobileAdTargetingInfo targetingInfo = MobileAdTargetingInfo(keywords: ["Promotion","Kids","coins"]);
  RewardedVideoAd rewardedVideoAd = RewardedVideoAd.instance;
  int coins;
  String appName;
  @override
  void initState() {
    super.initState();
    //"ca-app-pub-xxxxxxxxxxxx/5xxxxxxx75"
    rewardedVideoAd.load(adUnitId: "ca-app-pub-xxxxxxxxxxxx/5xxxxxx5");
  }
  @override
  Widget build(BuildContext context) {
    final pro = Provider.of<HomeModel>(context);
    return Scaffold(
      appBar: AppBar(
        title: Text("Download Apps"),
      ),
      body: FutureBuilder(
        future: stream.collection('apps').get(),
        builder: (context,snapshot){
          if(snapshot.hasData){
            return ListView.builder(
              itemCount: snapshot.data.docs.length,
              itemBuilder: (ctx,i){
                final List<DocumentSnapshot> documents = snapshot.data.docs;
                if(documents[i]['coins'] > 2){
                  return ListTile(
                    // subtitle: Text(documents[i]['userId']),
                    // ignore: deprecated_member_use
                    trailing: FlatButton(
                      color: Colors.green,
                      onPressed: ()async{
                        setState(() {
                          appName = documents[i]['link'];
                        });
                        await pro.downloadApp(documents[i]['link']);
                        showDialog(
                          context: context,
                          child: AlertDialog(
                            title: Text("Claim Your Coin"),
                            content: Text("its verify That you installed the app"),
                            actions: [
                              // ignore: deprecated_member_use
                              FlatButton(onPressed: ()async{
                                print(appName);
                                Navigator.pop(context);
                               bool installed = await DeviceApps.isAppInstalled(appName);
                               if(installed == true){
                                 print("installed=====================================");
                                 print(documents[i].id);
                                 await rewardedVideoAd.show();
                                 RewardedVideoAd.instance.listener =
                                     (RewardedVideoAdEvent event, {String rewardType, int rewardAmount}) {
                                   if (event == RewardedVideoAdEvent.rewarded) {
                                     setState(() {
                                       coins = rewardAmount;
                                     });
                                   }
                                 };
                                 print(coins);
                                 final pro = Provider.of<HomeModel>(context,listen: false);
                                await pro.addDownload(documents[i].id, coins);
                                 print("app called");
                               }else{
                                 print(documents[i].id);
                                 await rewardedVideoAd.show();
                                 RewardedVideoAd.instance.listener =
                                     (RewardedVideoAdEvent event, {String rewardType, int rewardAmount}) {
                                   if (event == RewardedVideoAdEvent.rewarded) {
                                     setState(() {
                                       coins = rewardAmount;
                                     });
                                   }
                                 };
                                 print("Not Installed");
                                 showDialog(
                                   context: context,
                                   child: AlertDialog(
                                     content: Text("You Must installed the app to claim your coins",style: TextStyle(color: Colors.red,),),
                                     title: Text("App Not Installed"),
                                     actions: [
                                       // ignore: deprecated_member_use
                                       FlatButton(onPressed: (){Navigator.pop(context);}, child: Text("Okay"))
                                     ],
                                   )
                                 );
                               }
                              }, child: Text("Claim"))
                            ],
                          )
                        );
                      },
                      child: Text("Download"),),
                    title: Text("https://play.google.com/store/apps/details?id=${documents[i]['link']}"),
                  );
                }else{
                  return Container();
                }
              },
            );
          }else{
            return Center(child: CircularProgressIndicator(),);
          }
        },
      ),
    );
  }
}

以下是错误代码:

Launching lib\main.dart on sdk gphone x86 64 in debug mode...
Running Gradle task 'assembleDebug'...
lib/views/downloads_apps.dart:92:36: Error: No named parameter with the name 'child'.
                                   child: AlertDialog(
                                   ^^^^^
/C:/src/flutter/packages/flutter/lib/src/material/dialog.dart:1035:12: Context: Found this candidate, but the arguments don't match.
Future<T?> showDialog<T>({
           ^^^^^^^^^^
lib/views/downloads_apps.dart:53:27: Error: No named parameter with the name 'child'.
                          child: AlertDialog(
                          ^^^^^
/C:/src/flutter/packages/flutter/lib/src/material/dialog.dart:1035:12: Context: Found this candidate, but the arguments don't match.
Future<T?> showDialog<T>({
           ^^^^^^^^^^


FAILURE: Build failed with an exception.

* Where:
Script 'C:\src\flutter\packages\flutter_tools\gradle\flutter.gradle' line: 991

* What went wrong:
Execution failed for task ':app:compileFlutterBuildDebug'.
> Process 'command 'C:\src\flutter\bin\flutter.bat'' finished with non-zero exit value 1

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 18s
Exception: Gradle task assembleDebug failed with exit code 1

2个回答

6
看起来你在 Alert/ShowDailog 中提到了 "child" 属性,请确保你的 Alert 和 ShowDailog 符合以下代码:
  return showDialog(
  context: context, // this context should be passed to Future Function as parameter
  builder: (BuildContext cx) {
    return AlertDialog(
      content: Text("Alert"),
      actions: [
        FlatButton(
            child: Text("Ok"),
            onPressed: () {
              Navigator.pop(cx);
            }),
      ],
    );
  },
);

请提供您的代码示例,以便理解您的意图。


0
  1. 进入 widgets.dart [缓存文件]
  2. 对于我来说,它位于 E:\flutter.pub-cache\hosted\pub.dartlang.org\nb_utils-3.1.4\lib\src
  3. 将每个 child 属性替换为 builder 属性,如下所示
  4. child: AlertDialog() >>>> builder: (context) => AlertDialog

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接