【问题标题】:Flutter Error: No named parameter with the name 'child'颤振错误:没有名为“孩子”的命名参数
【发布时间】:2021-02-28 15:01:17
【问题描述】:

我需要帮助,当我在 android studio 中运行以下代码时,出现以下错误:错误:没有名为 'child' 的命名参数

下面我也贴出了android studio生成的错误信息。

我运行了颤振升级,现在我收到一条错误消息 - [dart] 未定义命名参数“孩子”。新建项目,默认代码不变,但还是一样的错误:

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

【问题讨论】:

    标签: flutter


    【解决方案1】:

    看起来您在 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);
                }),
          ],
        );
      },
    );
    

    请提供您的代码示例以了解您的尝试。

    【讨论】:

      【解决方案2】:

      showDialog 小部件采用 context 和一个 builder。在构建器中,我们为 AlertDialog 小部件提供了标题、内容(标题的描述)和操作(是或否按钮)

      在您的代码中,您使用了 child 而不是 builder 这就是出现此错误的原因,要修复它,请使用 showDialog 中的构建器

              showDialog(
                  context: context,
                  builder: (ctx) => AlertDialog(
                    title: Text("Claim Your Coin"),
                    content: Text("its verify That you installed the app"),
                    actions: <Widget>[
                      FlatButton(
                        onPressed: () {
                          Navigator.of(ctx).pop();
                        },
                        child: Text("Okay"),
                      ),
                    ],
                  ),
                );
      

      【讨论】:

        猜你喜欢
        • 2021-10-06
        • 1970-01-01
        • 2021-04-26
        • 2021-01-05
        • 1970-01-01
        • 2021-04-26
        • 2020-01-03
        • 2021-03-24
        • 2021-12-11
        相关资源
        最近更新 更多