如何使用dart / flutter在不同类上使用变量

问题描述 投票:0回答:2

我有一个类是我的getToken类。在这个类中,我得到了令牌,这是令牌。这是我的getToken.dart

class GetToken {
  String token;
  Future<Null> getData() async {
    var url = "http://192.168.1.39:7070/api/v2/token";
    http.post(url, body: {
      "grant_type": "string",
      "branchcode": "string",
      "password": "string",
      "username": "string",
      "dbname": "string",
      "dbuser": "string",
      "dbpassword": "string",
      "dbtype": "string"
    }).then((response) {
      print("Response Status: ${response.statusCode}");
      //print("Response Body: ${response.body}");
      print('access token is -> ${json.decode(response.body)['access_token']}');
      token = json.decode(response.body)['access_token'];
    });
  }
}

我想在我的getCari类中使用此标记,并在我的rest api中获取Json值。这是我的getCari.dart类

class GetCari{
  getCari() async {
    final response = await http.get("http://192.168.1.39:7070/api/v2/ARPs",
    headers: {HttpHeaders.AUTHORIZATION: token});

    if(response.statusCode ==200){
      return Cari.fromJson(json.decode(response.body));
    }else{
      throw Exception("Failed to Load");
    }
  }
}

我想问一下如何在我的getCari.dart类中使用我的令牌(来自getToken.dart)。怎么能通过令牌变量其他类?

dart
2个回答
1
投票

请使用Dart的顶级函数,而不是不需要实例化的类。

这在Effective Dart documentation中提到过

token_manager.dart

String _token;
String get token => _token    // To ensure readonly

Future<Null> setToken() async {
  // set your _token here
}

get_cari.dart

import 'token_manager.dart' as token_manager    // use your import path here

getCari() async {
  // You can now access token from token_manager from any class like this.
  final String token = token_manager.token;
}

摘抄

在Java和C#中,每个定义都必须在一个类中,因此通常会看到“类”只存在于填充静态成员的位置。其他类用作命名空间 - 一种为一堆成员提供共享前缀以使它们彼此关联或避免名称冲突的方法。

Dart具有顶级函数,变量和常量,因此您不需要一个类来定义某些东西。如果您想要的是命名空间,那么库更适合。库支持导入前缀和显示/隐藏组合器。这些是强大的工具,让代码的使用者以最适合他们的方式处理名称冲突。

如果函数或变量在逻辑上与类无关,请将其置于顶层。如果您担心名称冲突,请为其指定更精确的名称,或将其移动到可以使用前缀导入的单独库中。


0
投票

你可以通过create对象fromGetCari()类将数据传递给getCari方法,看看这个

class GetToken {
  String token;
  Future<Null> getData() async {
    var url = "http://192.168.1.39:7070/api/v2/token";
    http.post(url, body: {
      "grant_type": "string",
      "branchcode": "string",
      "password": "string",
      "username": "string",
      "dbname": "string",
      "dbuser": "string",
      "dbpassword": "string",
      "dbtype": "string"
    }).then((response) {
      print("Response Status: ${response.statusCode}");
      //print("Response Body: ${response.body}");
      print('access token is -> ${json.decode(response.body)['access_token']}');
      token = json.decode(response.body)['access_token'];
      GetCari().getCari("fdfd");
    });
  }
}

class GetCari{

  getCari(String token) async {
    final response = await http.get("http://192.168.1.39:7070/api/v2/ARPs",
        headers: {HttpHeaders.AUTHORIZATION: token});

    if(response.statusCode ==200){
      return Cari.fromJson(json.decode(response.body));
    }else{
      throw Exception("Failed to Load");
    }
  }
© www.soinside.com 2019 - 2024. All rights reserved.