Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
251 views
in Technique[技术] by (71.8m points)

flutter - Data not passing to second screen while using constructor

I was trying to send data got from a JSON response to a second screen from the main screen.
My widgets are placed in separate class files. I called a single API call to fetch all data from the API so that there won't be many API calls happening while opening the app.

But I can't pass the data to the second screen in arrMaincategories. What should I do?

class HomeScreen extends StatefulWidget {
      @override
      _HomeScreenState createState() => _HomeScreenState();
    }
    
    class _HomeScreenState extends State<HomeScreen> {
    var arrBanner = [];
      var arrImages = [];
      var arrMainCategories = [];
      @override
      void initState() {
        super.initState();
        this.getBanner();
    
        setState(() {});
      }
    
      getBanner() async {
        String url = "https://www.clickxpress.in/api/";
    
        var prefs = await SharedPreferences.getInstance();
    
        var custID = prefs.getString('id');
    
        var response = await http.post(Uri.encodeFull(url),
            body: (<String, String>{
              'type': "homepage",
              'secKey': "2a067feaeb67fgh89fyrc6657654fhjj9714b2094818f",
              'customerId': custID,
              'geoLat': "55.2324",
              'geoLong': "23.55556",
            })); //for accepting only json response
        setState(() {
          Map<String, dynamic> map = json.decode(response.body);
          var data = map["attributes"];
    arrBanner = data['topBanner'];
          arrMainCategories = data['mainCategories'];
    if (arrBanner != null) {
            for (var value in arrBanner) {
              final image = value['imageUrl'];
              arrImages.add(NetworkImage(image));
            }

Called It to a new screen

NewCat(arrMainCategories: arrMainCategories),

trying to fetch on 2nd screen

class NewCat extends StatefulWidget {
  NewCat({Key key, @required this.arrMainCategories}) : super(key: key);
  final arrMainCategories;

  @override
  _NewCatState createState() => _NewCatState(arrMainCategories);
}

  
class _NewCatState extends State<NewCat> {
  var arrMainCategories = [];
  var arrCatImages = [];
  var arrCatName = [];

  _NewCatState(
    arrMainCategories,
  );

But the value is not coming here in the empty array. what to do?

question from:https://stackoverflow.com/questions/65623744/data-not-passing-to-second-screen-while-using-constructor

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Instead of using the Constructor, You Can also access the data by using this line of code

print(widget.arrMainCategories);

See this resource link for more details


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...