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






so how is the best way to overcome this problem?

– Denis Ramdan
Sep 14 '18 at 7:48






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.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)