Extract data from flutter

52 views Asked by At

I'm building an app with Flutter and a backend server with a Python Flask server..

The data returned from the backend server is in the following format:

(('helloworld',), ('hello',))

I want to extract helloworld and hello from this data.

However, sometimes there is only one piece of data, sometimes two, and sometimes more.

How can I extract data? Below is my code.

import 'package:flutter/material.dart';
import 'package:dio/dio.dart';
import 'dart:core';

class messageview extends StatefulWidget {
  @override
  mymessageview createState() => mymessageview();
}

class mymessageview extends State<messageview> {
  void _loadmydata() async {
    final dio = Dio();
    final response = await dio.get('http://127.0.0.1:3000/mymessage');
    //Here we need code to extract data.
  }
  
  @override
  void initState() {
    super.initState();
    _loadmydata();
  }
  
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Text("My Messasge View")
      ),
    );
  }
}

If you know a way to solve this problem, please let me know how to solve it.

1

There are 1 answers

0
MenTalist On

You can modify your _loadmydata() function to extract the data:

void _loadmydata() async {
    final dio = Dio();
    final response = await dio.get('http://127.0.0.1:3000/mymessage');
    //Here we need code to extract data.

   List<String> extractedData = [];
  
  // Extract data from the response
  List<dynamic> responseData = response.data;

  for (var item in responseData) {
    if (item is List && item.isNotEmpty) {
      // Extract the first element and add it to the list
      extractedData.add(item[0]);
     }
   }
  }