dart

How to create a static extension method in Dart?

人盡茶涼 提交于 2021-02-05 09:09:52
问题 I'm trying to create a static extension method on one of my classes (which is autogenerated, so I can't easily modify it). According to the docs, this should be possible: Extensions can also have static fields and static helper methods. Yet even this small example does not compile: extension Foo on String { static String foo() => 'foo!'; } void main() { print(String.foo()); } Error: Method not found: 'String.foo'. print(String.foo()); ^^^ What am I doing wrong? 回答1: The docs mean that the

How to avoid race condition?

独自空忆成欢 提交于 2021-02-05 08:59:05
问题 I am make random 1:1 chat app with Flutter and Firestore. But I have race condition when I am connect second user to chat. This my client app code for add second user to Firestore (first user is already add to Firestore document): await chatRoomReference.setData({ ‘secondUserUID': uid, }); When second user tap to chat I am remove option to enter this chatroom from all client UI. But is possible that if third user tap to chat at same time (before UI get update from stream) he will also be add

Dynamically setting variable name in Dart

喜你入骨 提交于 2021-02-05 08:36:27
问题 I had saved a variables name and value to a JSON file in Dart. Later I extracted the name and value from that JSON file and now am trying to create a new variable with that name. Something like this: var variableName= "firstName"; String variableName = "Joe"; so that: String firstName = "Joe"; Is there a way to do this? 回答1: Short answer: No. You cannot create variables at runtime in Dart. The compiler assumes that all variables are visible when the program (or any single method) is compiled.

setState() doesn't execute until the enclosing function returns

青春壹個敷衍的年華 提交于 2021-02-05 08:27:29
问题 So I was trying this code in flutter: void main() => runApp(MyApp()); class MyApp extends StatefulWidget { @override _MyAppState createState() => _MyAppState(); } class _MyAppState extends State<MyApp> { int _i = 1; @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( body: Center( child: MaterialButton( child: Text('You Pressed Me $_i'), onPressed: () { setState(() { _i++; print('inside i = $_i'); }); sleep(Duration(seconds: 10)); _i++; print('outside i = $_i');

setState() doesn't execute until the enclosing function returns

♀尐吖头ヾ 提交于 2021-02-05 08:27:28
问题 So I was trying this code in flutter: void main() => runApp(MyApp()); class MyApp extends StatefulWidget { @override _MyAppState createState() => _MyAppState(); } class _MyAppState extends State<MyApp> { int _i = 1; @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( body: Center( child: MaterialButton( child: Text('You Pressed Me $_i'), onPressed: () { setState(() { _i++; print('inside i = $_i'); }); sleep(Duration(seconds: 10)); _i++; print('outside i = $_i');

How to use List.sort on values returned from a function that contains an async?

夙愿已清 提交于 2021-02-05 08:18:51
问题 I have this code : widget.items.sort((a, b) { await getItemDistance(a, true); await getItemDistance(b, false); return (itemADistance) .compareTo(itemBDistance); }); I am trying to sort widget.items list based on values returned from getItemDistance. However I get an error with a red squiggly line that says : The await expression can only be used in an async function and when I try to add async to the sort method I get another red squiggly line that says : The argument type 'Future Function

multiple where query in firestore queryBuilder

可紊 提交于 2021-02-05 07:49:26
问题 I just noticed today that whenever I want to query a database I get an error which doesn't really make a sense to me and querying just stop working. It worked just fine few weeks ago. Invalid Query. All where filters with an inequality (lessThan, lessThanOrEqual, greaterThan, or greaterThanOrEqual) must be on the same field. But you have inequality filters on 'value1' and 'value2') Stream<List<MyModel>> myStream( {MyFilter filter, String category}) { return _service.collectionsStream( path:

multiple where query in firestore queryBuilder

杀马特。学长 韩版系。学妹 提交于 2021-02-05 07:49:20
问题 I just noticed today that whenever I want to query a database I get an error which doesn't really make a sense to me and querying just stop working. It worked just fine few weeks ago. Invalid Query. All where filters with an inequality (lessThan, lessThanOrEqual, greaterThan, or greaterThanOrEqual) must be on the same field. But you have inequality filters on 'value1' and 'value2') Stream<List<MyModel>> myStream( {MyFilter filter, String category}) { return _service.collectionsStream( path:

Flutter hot reload doesn't work on autosave in VSCode

我的未来我决定 提交于 2021-02-05 07:48:43
问题 Flutter hot reloads every time VS Code auto-saves my project before. A few hours ago, I started experiencing this issue where auto-saving doesn't hot-reload my app. Pressing Ctrl+S manually triggers hot reload but auto-saving does not. I've tried re-installing VS Code and Flutter SDK but the problem still exists. My newly installed VS Code only has the following extensions and settings: Dart: v3.19.1 Flutter: v3.19.0 Auto-save is set to afterDelay of 1000ms How do I hot reload when autosaving

How to display the countdown time for all users [closed]

旧街凉风 提交于 2021-02-05 07:46:24
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 8 months ago . Improve this question I am building a flutter auction application, I am stuck at the countdown time of an auction product so I use firebase realtime database. I have to make sure all users see the countdown at the same time. Please help me whether it's just a keyword to search