Flutter Error: How to pass data one widget class to another normal class?

Issue

I need to pass data s_id from Stateful class (TeamButton) to NetReq class .
Both class are in the same file.I do not understand how to do?
I do not understand how to do?I do not understand how to do?
How can I make it?

This is my stateful widget.

import 'dart:convert';

import 'package:fi_digital_earn/DashBoards/TeamFile/team_list.dart';
import 'package:fi_digital_earn/DashBoards/TeamFile/team_model.dart';
import 'package:flutter/material.dart';
import 'package:flutter/foundation.dart';
import 'package:http/http.dart' as http;

import '../../drawercontent.dart';

class TeamButton extends StatefulWidget {
  final s_id;

  const TeamButton({Key? key, this.s_id}) : super(key: key);

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

class _TeamButtonState extends State<TeamButton> {
  @override

  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Color(0xff392850),
        title: Row(
          // crossAxisAlignment: CrossAxisAlignment.center,

          // mainAxisAlignment: MainAxisAlignment.center,
          children: [
            InkWell(
                onTap: () {
                  Navigator.pop(context);
                },

                child: Icon(Icons.arrow_back)),
            SizedBox(
              width: 10.0,
            ),
            Text(

              "Teams",
              style: TextStyle(fontStyle: FontStyle.italic),
            ),
          ],
        ),

        actions: [
          IconButton(
            icon: Icon(Icons.person),
            onPressed: () => print("open cart"),
          ),

        ],
      ),
      drawer: Drawer(
        child: DrawerContent(),
      ),

      body: Column(
        children: [
          SizedBox(height: 50),
          Flexible(
            // ignore: missing_required_param

            child: FutureBuilder<List<Model>>(
              future: NetReq.fetchTeams(),
              builder: (context, snapshot) {
                if (snapshot.hasError) {
                  return Text("Error ${snapshot.error}");

                } else if (snapshot.hasData) {
                  return Container(
                      child: GridView.count(
                          childAspectRatio: 1.0,
                          padding: EdgeInsets.only(left: 20, right: 20),

                          crossAxisCount: 2,
                          crossAxisSpacing: 18,
                          mainAxisSpacing: 18,
                          children: snapshot.data!.map((team) {
                            return GestureDetector(

                              onTap: () {
                                print(team.teamType);
                                Navigator.push(
                                  context,
                                  MaterialPageRoute(

                                      builder: (context) => List1(
                                            teamUniqId: team.teamUniqId,
                                            teamType: team.teamType,
                                            user: team.user,
                                          )),

                                );
                              },
                              child: Container(
                                decoration: BoxDecoration(
                                    color: Color(0xffd8d7f5),

                                    borderRadius: BorderRadius.circular(10)),
                                child: Column(
                                  mainAxisAlignment: MainAxisAlignment.center,
                                  children: <Widget>[
                                    Center(

                                      child: Text(team.teamType,
                                          style: TextStyle(
                                              color: Colors.black,
                                              fontSize: 20,
                                              fontWeight: FontWeight.w600)),

                                    ),
                                  ],
                                ),
                              ),
                            );

                          }).toList()));
                }
                return Center(
                  child: CircularProgressIndicator(),
                );

              },
            ),
          ),
        ],
      ),

    );
  }
}

This is my another class which is below of stateful widget

class NetReq {
  static var url = Uri.parse(
      "https://www.a2rstore.in/api_mlm/v1/teamListApi.php?sid=123&uid=kkk");
  static List<Model> parseTeams(String responseBody) {
    var list = json.decode(responseBody) as List<dynamic>;
    List<Model> photos = list.map((model) => Model.fromJson(model)).toList();

    return photos;
  }

  static Future<List<Model>> fetchTeams() async {
    
    // final response = await http.get(url);
    var response = await http.get(url);
    if (response.statusCode == 200) {
      return compute(parseTeams, response.body);
    } else {

      throw Exception('Can\'t get Data');
    }
  }
}

Solution

You can do this using the widget param of the State class.

future: NetReq.fetchTeams(widget.s_id)

The widget parameter is available inside a State<T> class and it holds reference to your StatefulWidget which in your case is TeamButton.

Then, use it inside your fetchTeams function.

Since, you want to add the s_id into your main string, change your code like this.

static String baseUrl = "https://www.a2rstore.in/api_mlm/v1/teamListApi.php?sid=#sid&uid=kkk";

static Future<List<Model>> fetchTeams(var s_id) async {
    Uri url = Uri.parse(baseUrl.replaceFirst("#sid", s_id.toString());
    var response = await http.get(url);

Answered By – Nisanth Reddy

Answer Checked By – Cary Denson (FlutterFixes Admin)

Leave a Reply

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