I need format string like "Send %d seconds ago"
, "Harry like %s", "I think %1$s like %2$s"
. These can be implemented in Android, but i don't how to implement in Dart of Flutter.
相关问题
- What means in Dart static type and why it differs
- Flutter : Prepare list data from http request
- How to schedule an alarm on specific time in Flutt
- MappedListIterable is not a SubType
- 'firebase_messaging/FirebaseMessagingPlugin.h&
相关文章
- Observatory server failed to start - Fails to crea
- Flutter error retrieving device properties for ro.
- Adding Shadows at the bottom of a container in flu
- Flutter. Check if a file exists before loading it
- Flutter - http.get fails on macos build target: Co
- Receive share file intents with Flutter
- Do stateless widgets dispose on their own?
- How to clean your build with Flutter RP2 in Androi
Add the following to your
pubspec.yaml
then run pub install.
Next, import dart-sprintf:
import 'package:sprintf/sprintf.dart';
Example #
Output
Sends 5.00 seconds ago. Harry likes Cats, I think Dilki likes Dogs.
Source: https://pub.dartlang.org/packages/sprintf
If you want String interpolation similar to Android (
Today is %1$ and tomorrow is %2$
), you can create a top level function, or an extension that can do something similar. In this instance I keep it similar to Android strings as I'm currently porting an Android app (Interpolatation formatting starts with 1 rather than 0)Top Level Function
You can then call
interpolate(STRING_TO_INTERPOLATE, LIST_OF_STRINGS)
and you string would be interpolated.Extensions
You can create an extension function that does something similarish to Android
String.format()
This would then allow you to call
text.format(placeHolders)
Testing
Couple of tests for proof of concent:-
you could also have something simple like this:
Dart supports string interpolation
Also more complex expressions can be embedded with
${...}
The number types and the
intl
package provide more methods to format numbers and dates.See for example: