import 'package:intl/intl.dart';
void main(){
var myD = DateFormat('yyyy-MM-dd').format(DateTime.now());
List myList = List.filled(20,false);
myList.insert(0,myD.toString());
print(myList);
}
I have written the above code but there is no output or error showing. What is the issue I don't know?
it should be like this :
var myD ="jhjasashasdh";
final myList = List<String>.filled(3, "", growable: true);
myList.insert(0,myD.toString());
print(myList);
}
Related
I have string response like this, I got only below response of my api.
{authToken: msadnmsandnasdn}
and I have to convert as below.
{"authToken": "msadnmsandnasdn"}
So how i can do this please Help me.
You can use various manipulation operations to do that manually:
import 'dart:convert';
void main() {
var s = "{authToken: msadnmsandnasdn, name:risheek}";
var kv = s.substring(0,s.length-1).substring(1).split(",");
final Map<String, String> pairs = {};
for (int i=0; i < kv.length;i++){
var thisKV = kv[i].split(":");
pairs[thisKV[0]] =thisKV[1].trim();
}
var encoded = json.encode(pairs);
print(encoded);
}
Output:
{"authToken":"msadnmsandnasdn"," name":"risheek"}
You need to use jsonDecode on that string like this:
var response = {authToken: msadnmsandnasdn....};
var result = jsonDecode(response);
As you can see I have a list:
List avatarList = [
AssetsResources.VIP1,
AssetsResources.VIP2,
AssetsResources.VIP3,
AssetsResources.VIP4,
AssetsResources.VIP5,
AssetsResources.w1,
AssetsResources.w2,
];
I understand I can use method:
final bool isVIP = avatarList[index].contains('VIP');
But since AssetsResources.VIP1 is not a String like 'VIP1'but a path from other dart file, so here I have no idea how to check if the element from avatarList contains VIP value, thanks for any clue!
Update
Thanks guys for the help and sorry I didnt describe clearly, what I mean is, if
List idealList = [
'vip1',
'vip2',
'vip3',
'vip4',
'vip5',
];
so the elements in the idealList is 'vip1' but in my case the list myList is
List myList = [
AssetsResources.VIP1,
AssetsResources.VIP2,
AssetsResources.VIP3,
AssetsResources.VIP4,
AssetsResources.VIP5,
AssetsResources.w1,
AssetsResources.w2,
];
So it seems I can not directly use some methode as follows
final bool isVIP = myList[index].contains('VIP');
since the elements from myList is just a path(sorry I dont know how to call this value), could you please let me know in my case how to check if this path contains 'VIP' value? thanks!
Update
yes, AssetsResources is very simple, just store the asset path:
class AssetsResources {
/*worm avatar*/
static const String VIP1 = 'assets/worms/VIP_1.svg';
static const String VIP2 = 'assets/worms/VIP_2.svg';
static const String VIP3 = 'assets/worms/VIP_3.svg';
static const String VIP4 = 'assets/worms/VIP_4.svg';
}
The code should work fine :
class AssetsResources {
/*worm avatar*/
static const String VIP1 = 'assets/worms/VIP_1.svg';
static const String VIP2 = 'assets/worms/VIP_2.svg';
static const String VIP3 = 'assets/worms/VIP_3.svg';
static const String VIP4 = 'assets/worms/VIP_4.svg';
}
void main() {
List myList = [
AssetsResources.VIP1,
AssetsResources.VIP2,
AssetsResources.VIP3,
AssetsResources.VIP4,
];
for (final asset in myList) {
print(asset);
print(asset.contains('VIP'));
}
}
The above prints :
assets/worms/VIP_1.svg
true
assets/worms/VIP_2.svg
true
assets/worms/VIP_3.svg
true
assets/worms/VIP_4.svg
true
If I understood you correctly.
void main() {
for(var i = 0; i < avatarList.length; i++) {
String element = avatarList[i];
if(element.contains('VIP')) {
print(other.contains(element)); // true
print(other.firstWhere((e) => e.contains(element))); // 'VIP1', 'VIP2', 'VIP3', 'VIP4', 'VIP5'
}
}
}
List<String> avatarList = ['VIP1', 'VIP2', 'VIP3', 'VIP4', 'VIP5', 'w1', 'w2'];
List<String> other = ['VIP1', 'VIP2', 'VIP3', 'VIP4', 'VIP5', 'w1', 'w2'];
I've been looking for a WordPress + Flutter App integration and found a good one, but I got this error message:
I'm pretty this is a simple error, but I'm more into a design guy than a dev, so would be great if some of you give me some tip about it. Thanks in advance!
import 'dart:convert';
import 'package:http/http.dart' as http;
import '../config.dart';
import '../model/post_entity.dart';
class WpApi {
static const String BASE_URL = URL + REST_URL_PREFIX + '/wp/v2/';
static Future<List<PostEntity>> getPostsList(
{int category = 0, int page = 1}) async {
var posts = [];
try {
String extra = category != 0 ? '&categories=' + '$category' : '';
dynamic response = await http.get(Uri.parse(BASE_URL +
'''
posts?_embed&page=$page''' +
extra));
dynamic json = jsonDecode(response.body);
(json as List).forEach((v) {
posts.add(PostEntity.fromJson(v));
});
} catch (e) {
//TODO Handle No Internet Response
}
return posts;
}
static Future<List<PostCategory>> getCategoriesList({int page = 1}) async {
List<PostCategory> categories = [];
try {
dynamic response = await http.get(Uri.parse(BASE_URL +
'categories?orderby=count&order=desc&per_page=15&page=$page'));
dynamic json = jsonDecode(response.body);
(json as List).forEach((v) {
categories.add(PostCategory.fromJson(v));
});
} catch (e) {
//TODO Handle No Internet Response
}
return categories;
}
}
The error is on the return posts;
Exception has occurred.
_TypeError (type 'List<dynamic>' is not a subtype of type 'FutureOr<List<PostEntity>>')
change var posts = [] to List<PostEntity> posts = []
static Future<List<PostEntity>> getPostsList(
{int category = 0, int page = 1}) async {
List<PostEntity> posts = []; //<-- change var posts = [] to List<PostEntity> posts = []
try { ...
After reading a line from a file, I have the following String:
"[0, 1, 2, 3, 4]"
What is the best way to convert this String back to List<int>?
Just base on following steps:
remove the '[]'
splint to List of String
turn it to a int List
Sth like this:
List<int> list =
value.replaceAll('[', '').replaceAll(']', '')
.split(',')
.map<int>((e) {
return int.tryParse(e); //use tryParse if you are not confirm all content is int or require other handling can also apply it here
}).toList();
Update:
You can also do this with the json.decode() as #pskink suggested if you confirm all content is int type, but you may need to cast to int in order to get the List<int> as default it will returns List<dynamic> type.
eg.
List<int> list = json.decode(value).cast<int>();
You can convert String list to int list by another alternate method.
void main() {
List<String> stringList= ['1','2','3','4'];
List<int> intList = [];
stringList.map((e){
var intValue = int.tryParse(e);
intList.add(intValue!);
print(intList);
});
print(a);
}
Or by using for in loop
void main() {
List<String> stringList= ['1','2','3','4'];
List<int> intList = [];
for (var i in stringList){
int? value = int.tryParse(i);
intList.add(value!);
print(intList);
}
}
metas: "["<p>1</p>","<p>2</p>","<p>3/p>","<p>4</p>"]"
to
List<String> metas = ["<p>1</p>","<p>2</p>","<p>3/p>","<p>4</p>"]
I can use it in JS JSON.parse (meta), is there any way to do it in dart?
Use could use the jsonDecode function from dart:convert
import 'dart:convert';
void main() {
var x = '["<p>1</p>","<p>2</p>","<p>3/p>","<p>4</p>"]';
List metas = jsonDecode(x);
print(metas); // [<p>1</p>, <p>2</p>, <p>3/p>, <p>4</p>]
print(metas[0].runtimeType); // String
}