Class to Class Access Data

Issue

I have a problem with classes. I keep the user data in user_info_screen with Google auth. How can I pull this user data in a class that has this data on another page?

 class UserInfoScreen extends StatefulWidget {
   UserInfoScreen({Key key, User user})
      : _user = user,
        super(key: key);

  final User _user;

  @override
  _UserInfoScreenState createState() => _UserInfoScreenState();
}

class _UserInfoScreenState extends State<UserInfoScreen> {

  User _user;

The page I want to use (user) by pulling this data

class HomeView extends StatefulWidget {
  @override
  _HomeViewState createState() => _HomeViewState();
}

class _HomeViewState extends State<HomeView> {

}

Thank you.

Solution

A good way to handle it, espacially if you will use the user data on other pages, is to implement a store concept : https://pub.dev/packages/get

UserInfoScreen.dart

GetStorage box = GetStorage();
box.write('userData', _user);

HomeView.dart

GetStorage box = GetStorage();
User _user = box.read('userData');

Or pass it as an argument

UserInfoScreen.dart

Get.to(() => HomeView(), arguments: [
    {"userData": _user}
]);

HomeView.dart

  dynamic argumentData = Get.arguments;

  @override
  void onInit() {
    print(argumentData[0]['userData']);
    super.onInit();
  }

Answered By – BabC

Answer Checked By – Candace Johnson (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *