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

Posted

技术标签:

【中文标题】For 循环仅返回第一个 json 对象 Flutter【英文标题】:For loop returning only first json Object Flutter 【发布时间】:2020-06-01 03:22:38 【问题描述】:

虽然我试图从 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);
  

【问题讨论】:

您正在创建AccountInfo 的一个实例,然后在forloop 中您设置相同的变量而不是推入列表。你也应该把print(message)带入for bloc 【参考方案1】:

改变这个集团

     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 列表尝试一下(通过手动放置项目,例如:var list = ['itemA1':value, "itemA2":value,..., "itemB1": value] 让我知道它是否有效

以上是关于For 循环仅返回第一个 json 对象 Flutter的主要内容,如果未能解决你的问题,请参考以下文章

在 for 循环中更新变量

For循环仅返回最后一项

带有for循环的MVC视图仅返回最后一项

for循环遍历多级json数据

无法在 v-for 循环中显示数组中的所有对象,仅显示最后一个对象

jquery数组封装使用方法分享(jquery数组遍历)