0

Get variable data from a Stateful widget in another Stateful widget and they have no relation to one another (They are not children nor parents to each other)

I want to be able to access an array (posts) that is located in another file... You can see the comments in the code for more information

I have this stateful widget


    import 'package:flutter/material.dart';
    
    import 'package:saviortv/modules/fetchPosts.dart';
    import 'package:saviortv/ui/BuildPost.dart';
    
    class Home extends StatefulWidget {
      @override
      _HomeState createState() => _HomeState();
    }
    
    class _HomeState extends State<Home> {
      @override
      Widget build(BuildContext context) {
        return ListView.builder(
          itemCount: posts == null ? 0 : posts.length, // <== here posts is undefined because it doesn't exist as I want to get it from the other file (See below)
          itemBuilder: (BuildContext context, int index) {
            return buildPost(context, posts, index); //Building the posts list view
          },
        );
      }
    }

The other Stateful widget


class FetchPosts extends StatefulWidget {
  FetchPosts({Key key, this.pageNumber = 1}) : super(key: key);
  final int pageNumber;

  @override
  _FetchPostsState createState() => _FetchPostsState(pageNumber);
}

class _FetchPostsState extends State<FetchPosts> {
  _FetchPostsState(this.pageNumber);
  int pageNumber;
  List<wp.Post> posts = []; //<== I want this to be useable in the other file;

  Future<String> getPosts() async {
    var res = await fetchPosts(pageNumber);
    print(pageNumber);
    setState(() {
      posts = res;
    });
    return "Success!";
  }

  // Get _ when app loads;
  @override
  void initState() {
    super.initState();
    this.getPosts();
  }

  @override
  Widget build(BuildContext context) {
    return Container();
  }
}

How can I achieve that?

Hussein Al-Mosawi
  • 1,464
  • 3
  • 17
  • 37
  • 1
    If you need to get information from a widget's state into a completely unrelated widget's state, then that information shouldn't be associated with _any_ widget's state. It should be pulled out completely into its own reference class and accessed independently. – Abion47 Sep 18 '20 at 15:04
  • @Abion47 Can you make a demo representing that? and if it worked I will mark it as the correct answer... Thanks – Hussein Al-Mosawi Sep 18 '20 at 15:07
  • Use provider package or any other state management library to have the state available to different classes . The data class which should be available to other must come above other classes which will use it in the widget tree – Anurag Tripathi Sep 18 '20 at 15:14
  • @HusseinAl-Mosawi On how to make a class that other classes can reference? You need a demo of that? – Abion47 Sep 18 '20 at 15:21
  • 1
    Cant you just make them static,, or create a static function which returns the list so that you can get the list anywhere u want without an object – Bensal Sep 18 '20 at 15:30
  • Why don‘t you use a global variable? – w461 Sep 18 '20 at 23:06

0 Answers0