简体   繁体   English

Flutter Dart 将字符串从另一个列表变成列表

[英]Flutter Dart Turn String into List from another List

From an api i get a list of albums with cover art ids, I take that list and put the cover art ids into a function to get the URLs for the cover art from the api.从 api 我得到一个带有封面艺术 id 的专辑列表,我获取该列表并将封面艺术 id 放入 function 以从 Z8A5DA52ED120547D3A89E70AC 获取封面艺术的 URL。 My problem is I'm trying to turn the URLs into a list but i get the error: "type string is not a subtype of List".我的问题是我正在尝试将 URL 转换为列表,但出现错误:“类型字符串不是列表的子类型”。 If i don't try and turn it into a list the URLs are fine but I can't access individual urls so i can display it in a grid list.如果我不尝试将其转换为列表,则 URL 很好,但我无法访问单个 URL,因此我可以将其显示在网格列表中。 When i try and index it, it comes back with individual letters rather than the individual URLs.当我尝试将其编入索引时,它会返回单个字母而不是单个 URL。

class _RecentlyAddedAlbumsState extends State<RecentlyAddedAlbums> {
  Future<List<Album>> albums;
  List<String> coverARTLIST;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      /* backgroundColor: Colors.black, */
      body: Container(
        child: FutureBuilder(
            future: fetchRecentlyAddedAlbums(),
            builder: (context, AsyncSnapshot<List<Album>> data) {
              switch (data.connectionState) {
                case ConnectionState.none:
                  return Text(
                    "none",
                    style: TextStyle(color: Colors.black),
                  );
                case ConnectionState.waiting:
                  return Center(
                      child: CircularProgressIndicator(
                    valueColor: AlwaysStoppedAnimation<Color>(Colors.black),
                  ));
                case ConnectionState.active:
                  return Text('');
                case ConnectionState.done:
                  if (data.hasData) {
                    List<Album> albums = data.data;
                    return ListView.builder(
                      itemCount: albums.length,
                      itemBuilder: (context, index) {
                        return FutureBuilder(
                            future: recentAlbumArt(albums[index].coverArt),
                            builder: (context, AsyncSnapshot cover) {
                              switch (cover.connectionState) {
                                case ConnectionState.none:
                                  return Text(
                                    "none",
                                    style: TextStyle(color: Colors.black),
                                  );
                                case ConnectionState.waiting:
                                  return Center(
                                      child: CircularProgressIndicator(
                                    valueColor: AlwaysStoppedAnimation<Color>(
                                        Colors.black),
                                  ));
                                case ConnectionState.active:
                                  return Text('');
                                case ConnectionState.done:
                                  if (cover.hasData) {
                                    List<String> coverARTLIST = cover.data;
                                    print(coverARTLIST);
                                    return GridView.count(
                                      scrollDirection: Axis.vertical,
                                      crossAxisCount: 2,
                                      shrinkWrap: true,
                                      padding: const EdgeInsets.all(5),
                                      crossAxisSpacing: 10,
                                      children: <Widget>[Text("TEST")],
                                    );
                                  }
                              }
                            });
                      },
                    );
                  }
              }
            }),
      ),
    );
  }
}

coverart function:封面 function:

Future recentAlbumArt(String coverArtID) async {
  try {
    var salt = randomToken(6);
    var token = makeToken("$password", "$salt");
    var uRL =
        "$server/rest/getCoverArt/?u=$username&t=$token&s=$salt&v=$tapeOutVerison&c=$client$format&id=$coverArtID";
    return uRL.toString();
  } catch (e) {
    print(e);
  }
}

List<String> coverARTLIST = cover.data; This line is the reason for you error.此行是您出错的原因。 You are trying to assign string to list of string.您正在尝试将字符串分配给字符串列表。 Try this instead试试这个

List<String> coverARTLIST=[];
coverARTLIST.add(cover.data);
print(coverARTLIST[0]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM