Solved: Flutter Use Variable Outside of Function

Question

Asked by CidQu on January 01, 2022 (source).

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,
  ),
];

Answer

Question answered by Mohammad H (source).

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,
      ),
    ];
}
DART FLUTTER
SHARE: