我如何在Flutter内部的url中包含变量 [英] How do i include variables in url inside of Flutter

查看:293
本文介绍了我如何在Flutter内部的url中包含变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将电子邮件的密码值放在1111和22222插入的url的中间.电子邮件和密码值来自文本控制器,并存储在电子邮件和密码字符串中.我如何将它们放在url的中间. 我累了1111年的$ {email} 它给了错误,只有静态成员可以初始化

I want to put the value of email an password in the middle of the url insted of 1111 and 22222. The email and password values comes form text controllers and stored in email and password string. How can i put them in middle of url. I tired ${email} insted of 1111 it gave error only static member can be initialized

String email = "";
String password = "";
String url ="http://2i6b753b.ngrok.io/authenticate/1111/22222";

推荐答案

您的问题是不够的,因为您没有提供任何其他用法信息,但是根据Only static members can be initialized错误,我会假设您正在使用StatefulWidget(不是StatelessWidget,因为变量未使用关键字final初始化).

Your question is insufficient as you do not provide any other information on the usage, but according to the Only static members can be initialized error, I will answer assuming that you are using this in a StatefulWidget (not StatelessWidget because variables are not initialized with the keyword final).

您的代码

String email = "";
String password = "";
String url ="http://ngrok.io/authenticate/$email/$password";

String email = "";
String password = "";
String url ="http://ngrok.io/authenticate/${this.email}/${this.password}";

这是问题所在.
变量emailpassword是该类的成员(技术上是this).变量url也是该类的成员,它们都是初始化器.初始化程序在类的构造函数和超类的构造函数之前执行,并且仅允许在这些构造之后访问this. 因此,您不能在初始化期间使用初始化器的值.因此,您可以做什么,您可以使用静态成员.

Here is the problem.
Variable email and password are members of the class, (technically of this). The variable url is also a member of the class and they are all initializers. Initializers are executed before the constructor of the class, and the constructor of the super class, and this is only allowed to be accessed after these constructions. So, you can't use the value of the initializers during initialization. So, what can you do, you can use static members.

静态成员是属于静态或非静态类本身而不是该类对象的数据成员(变量)或方法.

Static members are data members (variables) or methods that belong to a static or a non static class itself, rather than to objects of the class.

根据Google的说法,这些静态成员不是您所需要的用例,因为emailpassword成员对于类的每个实例都应该有所不同.因此,我建议的最佳解决方法是将url用作函数或getter而不是像

According to Google, these static members are not the ones you want in your use case, as email and password members should be different for each instance of the class. So, the best fix I can advise is to use url as a function or a getter and not as a member, like

String get url => "http://ngrok.io/authenticate/$email/$password";

希望对您有帮助.

这篇关于我如何在Flutter内部的url中包含变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆