0

尝试在 TextField 和 onPressed 上的局部变量上设置一些值,以获取 TextField 上给出的局部变量和第二个变量,同时单击 onPressed 以工作两个函数,一个是 doSomething,另一个是 fetchBalance 函数。第一个函数 doSomthig 工作正常,但 fetchBalance 函数响应体打印成功,但内部条件未执行,但程序代码未出现任何错误。现在,我坚持这一点,请帮助我。

这是我们的变量

var _addressControler= TextEditingController();
  var url1='This is default variable ';
  var url2='noAddress entered';

第一个功能

doSomething(){
    setState(() {
      if(_addressControler.text == _addressControler.text){
        url2 = _addressControler.text.toString();
      }else{
        url2=url2;
        print("$url2");
      }
      print("$url2");
    });
  }

这是平衡的局部变量

 late Future<Balance> futureBalance;
  final _formKey = GlobalKey<FormState>();

  @override
   void initState() {
   super.initState();
  futureBalance = fetchBalance();
  }

 Future<Balance> fetchBalance() async {
    print("$url1");
    print("$url2");
    var finalAddress=url1+url2;
    print("$finalAddress");
    final response =
    await http.get(Uri.parse(finalAddress));

    print(response.body);

    if (response.statusCode == 200) {
      return Balance.fromJson(jsonDecode(response.body));
    } else {
      throw Exception('Failed to load album');
    }
  }

 @override Widget build(BuildContext context) {

return Scaffold(
  backgroundColor: Colors.amberAccent,
  appBar: AppBar(
    title: const Text('Retrieve Text Input'),
  ),
  body: Padding(
    key: _formKey,
    padding: const EdgeInsets.all(16.0),

    child: Column(
      children: [
        Padding(
          padding: const EdgeInsets.all(10.0),
          child: TextField(
            controller: _addressControler,
            decoration: InputDecoration(labelText: 'Enter the address...',
            labelStyle: TextStyle(color: Colors.blue),border: new OutlineInputBorder(
                borderSide:new BorderSide(color: Colors.black)),),
          ),
        ),

        Padding(
          padding: const EdgeInsets.symmetric(vertical: 16.0),
          child: ElevatedButton(
            onPressed:() async{
              doSomething();
              await fetchBalance();
            },
            child: const Text('Submit'),
      throw Exception('Failed to load album');
    }
  }

这是我们的用户界面基础

@override
  Widget build(BuildContext context) {
    
    return Scaffold(
      backgroundColor: Colors.amberAccent,
      appBar: AppBar(
        title: const Text('Retrieve Text Input'),
      ),
      body: Padding(
        key: _formKey,
        padding: const EdgeInsets.all(16.0),

        child: Column(
          children: [
            Padding(
              padding: const EdgeInsets.all(10.0),
              child: TextField(
                controller: _addressControler,
                decoration: InputDecoration(labelText: 'Enter the address...',
                labelStyle: TextStyle(color: Colors.blue),border: new OutlineInputBorder(
                    borderSide:new BorderSide(color: Colors.black)),),
              ),
            ),

            Padding(
              padding: const EdgeInsets.symmetric(vertical: 16.0),
              child: ElevatedButton(
                onPressed:() async{
                  doSomething();
                  await fetchBalance();
                },
                child: const Text('Submit'),
              ),
            ),
            Padding(
              padding: const EdgeInsets.symmetric(vertical: 16.0),
              child: Text("$url2"),
            ),
            Container(
              child: FutureBuilder<Balance>(
              future: futureBalance,
               builder: (context, snapshot) {
               if (snapshot.hasData) {
               print(snapshot.data!.height);
              print(snapshot.data!.result[0].amount);
               var x = (snapshot.data!.result[0].amount);
               var y =int.parse(x);
                assert(y is int);
                  ),
                ],
              );
                 } else if (snapshot.hasError) {
                 return Text("${snapshot.error}");
                 }
                 // By default, show a loading spinner.
                 return CircularProgressIndicator();
               },
            ),

            ),
          ],
        ),
      )
    );
  }
 
}
            print(z);
               return
               Column(
                children: [
                  Padding(
                    padding: const EdgeInsets.all(16.0),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Text('${z.toString()}',style: TextStyle(fontSize: 20,),),
                      ],
                    ),
                  ),
                ],
              );
                 } else if (snapshot.hasError) {
                 return Text("${snapshot.error}");
                 }
                 // By default, show a loading spinner.
                 return CircularProgressIndicator();
               },
            ),

            ),
          ],var _addressControler= TextEditingController();
 
        ),
      )
    );
  }
 
}
4

2 回答 2

0

你不需要 url2 变量只需_addressControler.text发送fetchBalance()

 Future<Balance> fetchBalance() async {  
    final response =
    await http.get(Uri.parse(url1 +_adressController.text));
    if (response.statusCode == 200) {
      return Balance.fromJson(jsonDecode(response.body));
    } else {
      throw Exception('Failed to load album');
    }
  }
于 2021-08-19T11:46:25.333 回答
0

它正在工作...

child: ElevatedButton(
                        onPressed: ()  {
                          setState(() {
                            fetchBalance();
                          });
                          fetchBalance();
                        },
                        child: const Text('Submit'),
                      ),
                    ),
于 2021-08-25T05:01:15.243 回答