首页 > 解决方案 > How can I access global data from another class in Flutter?

问题描述

I have a simple class like;

import 'dart:io';
class IDCardClass {
  File frontImageFile;
  File backImageFile;
}

From front_test.dart class I need to assign a data to frontImageFile and back_test.dart class I need to assign a data to backImageFile.

In my home.dart or another ***.dart class I need to get frontImageFile and backImageFile to show it to user.

My question is How can I access global data from another class in Flutter?

标签: classflutterglobal-variables

解决方案


使变量static像这样:

class IDCardClass {
  static File frontImageFile;
  static File backImageFile;
}

只需将该类导入另一个类并访问变量。

import 'package:your_projectname/your_folder/IDCardClass.dart';

.....

var imageFile = IDCardClass.frontImageFile;

.....

推荐阅读