json - For循环仅返回第一个json对象Flutter

标签 json listview flutter dart

当我尝试从JSON获取数据时,只能返回第一个对象。
我提到了我认为问题所在的声明。我想从JSON返回所有可用的对象,以便将其显示在卡列表中。我需要返回所有JSON对象,并将其传递到包含卡片列表的另一页。如果有人知道解决方案,请帮助我修复它。
提前致谢。

import 'dart: convert';
import 'package:flutter/cupertino.dart';
import 'package:tts/tts.dart';
import 'package:wiizboo/service/Chatmsg_service.dart';
import 'package:wiizboo/widget/a%20copy.dart';
import 'package:wiizboo/widget/chat_in_message.dart';
import 'package:wiizboo/widget/chat_out_message.dart';
import 'package:wiizboo/widget/form.dart';
import 'package:wiizboo/widget/image_camera.dart';
import 'package:wiizboo/widget/image_gallery.dart';

class ChatMessageModel with ChangeNotifier {
  String data;
  List accntdet;

  ChatmessageService chatservice = ChatmessageService();
  List<Widget> messages = <Widget>[];

  ChatMessageModel() {
    sendMsg("Hi");
  }

  Future handlesubmission(String chattext) {
    Widget message = new ChatInMessage(
      text: chattext,
      name: "Me",
      type: true,
    );
    addMsg(message);
    sendMsg(chattext);
  }

  addMsg(Widget msg) {
    messages.insert(0, msg);
    notifyListeners();
  }

  sendMsg(String msg) {
    chatservice.SendMsg(msg).then((String msg) {
      responseBuilder(msg);
    });
  }

  responseBuilder(String msg) {
    Widget message;
    String identifier = '';

    var arr = msg.split("~");
    if (arr.length > 1) {
      identifier = arr[0];
      msg = arr[1];
    } else {
      msg = arr[0];
    }

    switch (identifier) {
      case 'email_phone':
        message = new Forms(onSubmitted: (String val) {
          Widget a = new ChatInMessage(
            text: val,
            name: "Me",
            type: true,
          );
          addMsg(a);
          sendMsg(val);
        });
        break;

      case 'selfie':
        message = new ImageCamera(onSubmitted: (String imageres) {
          Widget b = new ChatInMessage(
            text: imageres,
            name: "Me",
            type: true,
          );
          sendMsg(imageres);
        });
        break;

      case 'aadhar':
        message = new ImageGalery(onSubmitted: (String imageres) {
          Widget b = new ChatInMessage(
            text: imageres,
            name: "Me",
            type: true,
          );
          sendMsg(imageres);
        });
        break;

      case 'account_info':
        print(msg);
        data = msg;
        String receivedJson = data;
        List<dynamic> list = json.decode(receivedJson);
        accntdet = list;
        int l = list.length;
        print(l);

//------------ the statement --------//
        for (dynamic account in accntdet) {
          message = new AccountInfo(
            l: l,
            iban: account['ibn_no'],
            accno: account['account_no'],
            sort: account['sort-code'],
            currency: account['currency'],
          );
        }
//----------//

        print(message);
        break;

      default:
        message = new ChatOutMessage(
          text: msg,
          name: "WzBoo..",
        );
        Tts.speak(msg);
    }

    addMsg(message);
  }
}

最佳答案

改变这个集团

     class ChatMessageModel with ChangeNotifier {
  String data;
  List accntdet;

  ChatmessageService chatservice = ChatmessageService();
  List<Widget> messages = <Widget>[];

  ChatMessageModel() {
    sendMsg("Hi");
  }

  Future handlesubmission(String chattext) {
    Widget message = new ChatInMessage(
      text: chattext,
      name: "Me",
      type: true,
    );
    addMsg(message);
    sendMsg(chattext);
  }

  addMsg(Widget msg) {
    messages.insert(0, msg);
    notifyListeners();
  }

  sendMsg(String msg) {
    chatservice.SendMsg(msg).then((String msg) {
      responseBuilder(msg);
    });
  }

  responseBuilder(String msg) {
    Widget message;
    String identifier = '';

    var arr = msg.split("~");
    if (arr.length > 1) {
      identifier = arr[0];
      msg = arr[1];
    } else {
      msg = arr[0];
    }

    switch (identifier) {
      case 'email_phone':
        message = new Forms(onSubmitted: (String val) {
          Widget a = new ChatInMessage(
            text: val,
            name: "Me",
            type: true,
          );
          addMsg(a);
          sendMsg(val);
        });
        break;

      case 'selfie':
        message = new ImageCamera(onSubmitted: (String imageres) {
          Widget b = new ChatInMessage(
            text: imageres,
            name: "Me",
            type: true,
          );
          sendMsg(imageres);
        });
        addMsg(message);
        break;

      case 'aadhar':
        message = new ImageGalery(onSubmitted: (String imageres) {
          Widget b = new ChatInMessage(
            text: imageres,
            name: "Me",
            type: true,
          );
          sendMsg(imageres);
        });
        break;

      case 'account_info':
        print(msg);
        data = msg;
        String receivedJson = data;
        List<dynamic> list = json.decode(receivedJson);
        accntdet = list;
        int l = list.length;
        print(l);

//------------ the statement --------//
        for (dynamic account in accntdet) {
          message = new AccountInfo(
            l: l,
            iban: account['ibn_no'],
            accno: account['account_no'],
            sort: account['sort-code'],
            currency: account['currency'],
          );
        print(message);
        addMsg(message);
        }
//----------//


        break;

      default:
        message = new ChatOutMessage(
          text: msg,
          name: "WzBoo..",
        );
        Tts.speak(msg);
        addMsg(message);
    }


  }
}

关于json - For循环仅返回第一个json对象Flutter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60258304/

相关文章:

Android:检查多个项目ListView

android - 刷新 fragment 中 listView 的 Item 的 TextSize

json - Dart 的解析问题 - [无构建值 ^ 可序列化 ^ ]

flutter - 参数 'id' 因其类型不能有 'null' 的值,但隐含的默认值为 'null'

python - 如何拆分数组中的字符串

安卓工作室 : How to parse a JSON Object from Inside of a JSON Array?

android - 在隐藏键盘或按下返回之前,带过滤器的 ListView 不会刷新

android - 有没有办法在 flutter 中为 DropdownButton 添加标签?

python - 使用 python 3 将 CSV 转换为 JSON

arrays - 在 Swift 4 中解码没有 key 的 JSON