简体   繁体   English

“String”类型不是“index”类型“int”的子类型相关的导致错误的小部件是 FutureBuilder<dynamic></dynamic>

[英]type 'String' is not a subtype of type 'int' of 'index' The relevant error-causing widget was FutureBuilder<dynamic>

I am getting error at this point.我在这一点上遇到错误。 The result works fine but it shows error for a second.结果工作正常,但它显示错误一秒钟。 I think this is because i'm using FutureBuilder inside a FutureBuilder.我认为这是因为我在 FutureBuilder 中使用 FutureBuilder。 I need to call two methods at 'future:' so instead of that i used another FutureBuilder but it is showing error.我需要在“未来:”调用两个方法,所以我使用了另一个 FutureBuilder,但它显示错误。

sendOfferButton() {
    return FutureBuilder(
      initialData: [],
      future: getUserProfile(),
      builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot) {
        cnicCheck = snapshot.data['CNIC'];
        return RaisedButton(
          padding: EdgeInsets.symmetric(vertical: 10),
          child: Text('Send Offer'),
          textColor: Colors.white,
          color: Colors.green,
          onPressed: () {
            if (cnicCheck == "verified") {
              Navigator.push(
                context,
                MaterialPageRoute(
                  builder: (_) => SendOffer(),
                ),
              );
            } else {
              Navigator.push(
                context,
                MaterialPageRoute(
                  builder: (_) => VerifyCNIC(),
                ),
              );
            }
          },
        );
      },
    );
  }
Future getUserProfile() async {
  DocumentSnapshot document = await FirebaseFirestore.instance
      .collection('Users')
      .doc(FirebaseAuth.instance.currentUser.email)
      .get();
      return document;
  
}

You can use FutureBuilder inside FutureBuilder without any problem.您可以在 FutureBuilder 中使用 FutureBuilder 没有任何问题。 You are getting error because you are trying to access the data before it is ready.您收到错误是因为您试图在数据准备好之前访问它。 Try this.尝试这个。

FutureBuilder(
    future: getUserProfile(),
    builder: (context, snapshot) {
      if (snapshot.hasData) {
        final cnicCheck = snapshot.data['CNIC'];
        return RaisedButton(
          padding: EdgeInsets.symmetric(vertical: 10),
          child: Text('Send Offer'),
          textColor: Colors.white,
          color: Colors.green,
          onPressed: () {
            if (cnicCheck == "verified") {
              Navigator.push(
                context,
                MaterialPageRoute(
                  builder: (_) => SendOffer(),
                ),
              );
            } else {
              Navigator.push(
                context,
                MaterialPageRoute(
                  builder: (_) => VerifyCNIC(),
                ),
              );
            }
          },
        );
      } else
        return CircularProgressIndicator();
    },
  );

I solved it by this我通过这个解决了

 Future<String> getCNIC() async {
  DocumentSnapshot document = await FirebaseFirestore.instance
      .collection('Users')
      .doc(FirebaseAuth.instance.currentUser.email)
      .get();
       String getCNIC = document['CNIC'];
      return getCNIC;

}
sendOfferButton() {
    return FutureBuilder<String>(
      initialData: cnicCheck,
      future: getCNIC(),
      builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot) {
        cnicCheck = snapshot.data;
        return RaisedButton(
          padding: EdgeInsets.symmetric(vertical: 10),
          child: Text('Send Offer'),
          textColor: Colors.white,
          color: Colors.green,
          onPressed: () {
            if (cnicCheck == "verified") {
              Navigator.push(
                context,
                MaterialPageRoute(
                  builder: (_) => SendOffer(),
                ),
              );
            } else {
              Navigator.push(
                context,
                MaterialPageRoute(
                  builder: (_) => VerifyCNIC(),
                ),
              );
            }
          },
        );
      },
    );
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 找不到 MediaQuery 小部件祖先。相关的导致错误的小部件是 FutureBuilder<FirebaseApp> - No MediaQuery widget ancestor found.The relevant error-causing widget was FutureBuilder<FirebaseApp> 相关的导致错误的小部件是 StreamBuilder <querysnapshot<object?> &gt; </querysnapshot<object?> - The relevant error-causing widget was StreamBuilder<QuerySnapshot<Object?>> 红屏错误类型“字符串”不是“索引”Flutter 的“int”类型的子类型 - Red Screen Error type 'String' is not a subtype of type 'int' of 'index' Flutter 异常“类型&#39;字符串&#39;不是&#39;索引&#39;的&#39;int&#39;类型的子类型” - Exception "type 'String' is not a subtype of type 'int' of 'index'" “String”类型不是“index”的“int”类型的子类型 - type 'String' is not a subtype of type 'int' of 'index' Flutter StreamBuilder小部件错误:类型'()=&gt; Map<string, dynamic> ' 不是类型 'DocumentSnapshot 的子类型<object?> ' 在类型转换中</object?></string,> - Flutter StreamBuilder widget Error : type ' () => Map<String, dynamic >' is not a subtype of type 'DocumentSnapshot<Object?>' in type cast &#39;列表<dynamic> &#39; 不是类型 &#39;(BuildContext, int) =&gt; Widget&#39; 的子类型 - 'List<dynamic>' is not a subtype of type '(BuildContext, int) => Widget' flutter 未处理的异常:键入“未来<dynamic> ' 不是 'String' 类型的子类型 - 使用 FutureBuilder.wait() 时</dynamic> - Unhandled Exception: type 'Future<dynamic>' is not a subtype of type 'String' - when using FutureBuilder.wait() 一个构建函数返回 null ,相关的导致错误的小部件是:StreamBuilder<EzuserData> - A build function returned null ,The relevant error-causing widget was : StreamBuilder<EzuserData> Flutter:未处理的异常:类型“String”不是“index”类型“int”的子类型 - Flutter : Unhandled Exception: type 'String' is not a subtype of type 'int' of 'index'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM