Sermed Berwari
Sermed Berwari

Reputation: 27

Can't use data coming from parameters in Future function

I have page send data's as parameter to other page ,second page receive the arguments inside (Widget build(BuildContext context)) ,So the Future function is at the start of page Class , like this code:

import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';

class Days extends StatefulWidget {
  @override
  _DaysState createState() => _DaysState();
}

class _DaysState extends State<Days> {
  var cm_id;
  var d_id;
  var w_id;
  var u_id;
  Map rdata = {};
  Map data = {};
  List digrees;
  Future getDigrees() async {
    var url = 'http://10.0.2.2/jiyan/test/api/digrees/day_digree.php?u_id=' +
        u_id +
        '&m_id=' +
        cm_id +
        '&d_id=' +
        d_id;
    var response = await http.get(url);
    data = jsonDecode(response.body);
  }
  @override
  void initState() {
    super.initState();
    getDigrees();
  }
  @override
  Widget build(BuildContext context) {
    rdata = ModalRoute.of(context).settings.arguments;
    setState(() {
      cm_id = int.parse(rdata['current_m_id'].toString());
      d_id = int.parse(rdata['d_id'].toString());
      w_id = int.parse(rdata['w_id'].toString());
      u_id = int.parse(rdata['u_id'].toString());
    });
    return Scaffold()

Now ,the function can't receive these data's ,How can i do that?

Upvotes: 0

Views: 42

Answers (1)

NiklasLehnfeld
NiklasLehnfeld

Reputation: 1080

  1. You should consider to not put everything as a membervariable in the state. Try to use dependency injection (putting everything you need as method arguments) as much as possible.

  2. String concatenation is easy in Dart using $ like:

int a = 5;
int b = 10;
String result = "$a + $b = ${a+b}";
  1. Here is the code I would suggest:
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';

class Days extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
      future: fetchDigrees(context),
      builder: (context, snapshot) {
        if (snapshot.connectionState != ConnectionState.done) {
          return Center(
            child: Text("Loading...."),
          );
        }

        List digrees = snapshot.data;

        return Text(digrees.length.toString());
      },
    );
  }

  Future<List> fetchDigrees(BuildContext context) async {
    Map rdata = ModalRoute.of(context).settings.arguments;

    int cm_id = int.parse(rdata['current_m_id'].toString());
    int d_id = int.parse(rdata['d_id'].toString());
    int w_id = int.parse(rdata['w_id'].toString());
    int u_id = int.parse(rdata['u_id'].toString());

    var url =
        'http://10.0.2.2/jiyan/test/api/digrees/day_digree.php?u_id=$u_id&m_id=$cm_id&d_id=$d_id';
    var response = await http.get(url);
    var data = jsonDecode(response.body);

    return data["digrees"]; //or however you have to extract this from the json
  }
}

Upvotes: 1

Related Questions