0

I'm tring to make data folder, but cannot add API to it. I can get results but can't add it to the List.

How can i get this variable outside of the Function? This .dart file only contain List, nothing more.

I want to get counts from the api and use them in my model list. This dart page is made for models only.

import 'package:selam/constants.dart';
import 'package:flutter/material.dart';
import 'dart:convert' as convert;
import 'package:http/http.dart' as http;
import 'package:selam/main.dart';
import 'package:shared_preferences/shared_preferences.dart';

class CloudStorageInfo {
  final String? title, totalStorage;
  final int? numOfFiles, percentage;
  final Color? color;
  final IconData? svgSrc;
  String? patientPhone;


  CloudStorageInfo({
    this.svgSrc,
    this.title,
    this.totalStorage,
    this.numOfFiles,
    this.percentage,
    this.patientPhone,
    this.color,
  });
}


Future<List<dynamic>> wiki() async {
var response = await http.get(Uri.https('www.h****y.com', '/wc-api/v3/orders/count?consumer_key=ck_3*3&consumer_secret=cs_6*0'));
print(response);
if (response.statusCode == 200) {
  var jsonResponse =
      convert.jsonDecode(response.body) as Map<String, dynamic>;
  print(jsonResponse);
  return jsonResponse['count'];
} else {
  print('Request failed with status: ${response.statusCode}.');
  return [];
}
}

List demoMyFiles = [
  CloudStorageInfo(
    title: "Kazanç",
    numOfFiles: 1328,
    svgSrc: Icons.monetization_on_outlined,
    totalStorage: "",
    color: primaryColor,
    percentage: 35,
  ),
  CloudStorageInfo(
    title: "Siparişler",
    numOfFiles: int.parse(wiki().toString()),
    svgSrc: Icons.shopping_bag_outlined,
    totalStorage: "",
    color: Color(0xFFFFA113),
    percentage: 35,
  ),
  CloudStorageInfo(
    title: "Kullanıcılar",
    numOfFiles: 1328,
    svgSrc: Icons.supervised_user_circle_outlined,
    totalStorage: "",
    color: Color(0xFFA4CDFF),
    percentage: 10,
  ),
  CloudStorageInfo(
    title: "İncelemeler",
    numOfFiles: 5328,
    svgSrc: Icons.reviews_outlined,
    totalStorage: "",
    color: Color(0xFF007EE5),
    percentage: 78,
  ),
];

1 Answer 1

1

You cant just simply move the variable outside of the function like this:

List<dynamic> result = [];

Future<void> wiki() async {
    var response = await http.get(Uri.https('www.h******y.com', '/wc- 
    api/v3/orders/count? 
    consumer_key=ck_3****3&consumer_secret=cs_6******0'));
    print(response);
    if (response.statusCode == 200) {
          var jsonResponse =
          convert.jsonDecode(response.body) as Map<String, dynamic>;
          print(jsonResponse);
          result = jsonResponse['count'];
    } else {
          print('Request failed with status: ${response.statusCode}.');
    }
}

And then pass the result value to your class.

You can also add a return type to your function and then call it later in the code:

Future<List<dynamic>> wiki() async {
    var response = await http.get(Uri.https('www.h******y.com', '/wc- 
    api/v3/orders/count? 
    consumer_key=ck_3****3&consumer_secret=cs_6******0'));
    print(response);
    if (response.statusCode == 200) {
          var jsonResponse =
          convert.jsonDecode(response.body) as Map<String, dynamic>;
          print(jsonResponse);
          return jsonResponse['count'];
    } else {
          print('Request failed with status: ${response.statusCode}.');
          return [];
    }
}

final List<dynamic> result = await wiki();

And then put it inside your object. (Of course it will need you to make the higher function async.

You have to add main() method to run the code:

void main() async {
    
    final List<dynamic> result = await wiki();
    List demoMyFiles = [
      CloudStorageInfo(
        title: "Kazanç",
        numOfFiles: 1328,
        svgSrc: Icons.monetization_on_outlined,
        totalStorage: "",
        color: primaryColor,
        percentage: 35,
      ),
      CloudStorageInfo(
        title: "Siparişler",
        numOfFiles: result.length,
        svgSrc: Icons.shopping_bag_outlined,
        totalStorage: "",
        color: Color(0xFFFFA113),
        percentage: 35,
      ),
      CloudStorageInfo(
        title: "Kullanıcılar",
        numOfFiles: 1328,
        svgSrc: Icons.supervised_user_circle_outlined,
        totalStorage: "",
        color: Color(0xFFA4CDFF),
        percentage: 10,
      ),
      CloudStorageInfo(
        title: "İncelemeler",
        numOfFiles: 5328,
        svgSrc: Icons.reviews_outlined,
        totalStorage: "",
        color: Color(0xFF007EE5),
        percentage: 78,
      ),
    ];
}
Sign up to request clarification or add additional context in comments.

4 Comments

I'm tring to do the second one, but I can't. It wants me to put in async, I don't think it is possible. I can't run any functions. final List<dynamic> result = await wiki(); The await expression can only be used in an async function. Try marking the function body with either 'async' or 'async*'.
You can make your main method async too. All dart codes must run inside a main method just like c++. But I dont see it in your code
I'm editing the main post, can you check it please? I'm adding the whole code.
I updated my answer to check it out

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.