简体   繁体   English

Flutter 错误:如何将一个小部件 class 的数据传递给另一个正常的 class?

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

I need to pass data s_id from Stateful class (TeamButton) to NetReq class.我需要将 Stateful class(TeamButton)中的数据 s_id 传递给 NetReq class。 Both class are in the same file.I do not understand how to do? class 都在同一个文件中。我不明白怎么办? 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 ,它位于有状态小部件的下方

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');
    }
  }
}

You can do this using the widget param of the State class.您可以使用State class 的widget参数来执行此操作。

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 . widget参数在State<T> class 中可用,它包含对您的StatefulWidget的引用,在您的情况下是TeamButton

Then, use it inside your fetchTeams function.然后,在您的fetchTeams function 中使用它。

Since, you want to add the s_id into your main string, change your code like this.由于您想将s_id添加到主字符串中,因此请像这样更改代码。

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Flutter - 如何在搜索时将搜索文本从一个小部件传递到另一个 class 小部件? - Flutter - How to pass search text from one widget to its another class widget while searching? 如何将数据从一个类传递到另一个类? - How to pass data from one class to another in flutter? TextField 不会将数据传递给同一类中的另一个 flutter Widget - TextField do not pass data to another flutter Widget in same class 如何使用 PageRouteBuilder 将数据从一个 class 传递到 flutter 中的另一个 class - How to pass data from one class to another class in flutter using PageRouteBuilder 如何将列表从一个 class 传递到另一个 Flutter? - How to pass a list from one class to another in Flutter? Flutter-如何在另一个类上显示/隐藏小部件 - Flutter - How to Show/Hide Widget on another Class 我如何将 QuerySnapshot 数据从一个小部件传递到 flutter 中的另一个小部件? - How i can pass QuerySnapshot data from one widget to another widget in flutter? 我如何将数据从一个小部件传递到 flutter 中的另一个小部件? - How i can pass Data from one widget to another widget in flutter? 如何将数据传递给 Flutter/Dart 中另一个屏幕的类? - How to pass data to the class of another screen in Flutter/Dart? 如何将实时数据传递给另一个 class 和 flutter 内的小部件 - How to pass live data to Widgets inside another class with flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM