Edit the Outer Variable With the Class Constructor
Edit the Outer Variable With the Class Constructor
how do i edit the chattile variable in the contructor 'tambah' as shown below:
class ChatCardList extends StatefulWidget
List<ChatTile> chattile; //EDIT THIS
ChatCardList(Key key, this.chattile) : super(key: key);
Future<void> _onSocketInfo_newMessage(dynamic data) async
ChatCardList _data = new ChatCardList.tambah(data);
ChatCardList.tambah(String message)
print("PESAN BARU : " + message);
this.chattile.add(message); //PUSH OBJECT
print(chattile); //notting added
@override
_ChatCardListState createState() => new _ChatCardListState(chattile:chattile);
why did that happen? is my script wrong, or is there something missing from my code?
many thanks,
1 Answer
1
Could you go back and check that this is the actual code that is causing your problem.
The code displayed here will not compile at all, so it's hard to say where the real problem is. Alternatively, describe in detail what you are actually trying to achieve.
The ChatCardList.tambah(String message) {
constructor does not initialize chattile
, so the value of this.chattile
in the body should be null
. Calling add
on that will throw. Otherwise that code is perfectly fine, it is how you add to a list stored in this
's chattile
variable.
ChatCardList.tambah(String message) {
chattile
this.chattile
null
add
this
chattile
The Future<void> _onSocketInfo_newMessage(dynamic data) async {
method creates a new ChatCardList
using CharCardList.tambah
, but never does anything with it (it doesn't return the value), so nobody will ever see that object or its chattile
value.
Future<void> _onSocketInfo_newMessage(dynamic data) async {
ChatCardList
CharCardList.tambah
chattile
Also, the print(chattile);
code is outside of any function or constructor body, so it is really declaring an abstract method called print
which takes a parameter named chattile
. Since your class isn't abstract, that should cause a compile-time error.
print(chattile);
print
chattile
So, one way to rewrite your program could be:
class ChatCardList extends StatefulWidget
List<ChatTile> chattile; //EDIT THIS
ChatCardList(Key key, this.chattile) : super(key: key);
Future<void> _onSocketInfo_newMessage(dynamic data)
String message = data;
this.chattile.add(message);
@override
_ChatCardListState createState() => new _ChatCardListState(chattile: chattile);
This code doesn't introduce the tambah
constructor, but instead updates the chattile
list immediately in the _onSocketInfo_newMessage
method. That method still needs to be called from somewhere.
tambah
chattile
_onSocketInfo_newMessage
I have to ask "which problem?" because it isn't clear what you are trying to do, so it is impossible to say what you should do to get there. I'll add a potential fix, but I can't say whether it does what you want.
– lrn
Sep 14 '18 at 13:46
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
so how is the best way to overcome this problem?
– Denis Ramdan
Sep 14 '18 at 7:48