Error: type 'Playlist' is not a subtype of type 'Items'

Asked

Viewed 302 times

1

Excerpt from the Model code:

  int numero;
  String localEntrega;
  String dataHora;
  String formaPagamento;
  List<Itens> itens;

  ListarPedido(
      {this.numero,
      this.localEntrega,
      this.dataHora,
      this.formaPagamento,
      this.itens});

  ListarPedido.fromJson(Map<String, dynamic> json) {
    numero = json['numero'];
    localEntrega = json['local_entrega'];
    dataHora = json['data_hora'];
    formaPagamento = json['forma_pagamento'];
    if (json['Itens'] != null) {
      itens = new List<Itens>();
      json['Itens'].forEach((v) {
        itens.add(new Itens.fromJson(v));
      });
    }
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['numero'] = this.numero;
    data['local_entrega'] = this.localEntrega;
    data['data_hora'] = this.dataHora;
    data['forma_pagamento'] = this.formaPagamento;
    if (this.itens != null) {
      data['Itens'] = this.itens.map((v) => v.toJson()).toList();
    }
    return data;
  }
}

class Itens {
  String itemUid;
  String qtde;
  String valor;
  String valorTotal;
  String descrProduto;
  String tamanho;
  String textoOpc;

  Itens(
      {this.itemUid,
      this.qtde,
      this.valor,
      this.valorTotal,
      this.descrProduto,
      this.tamanho,
      this.textoOpc});

  Itens.fromJson(Map<String, dynamic> json) {
    itemUid = json['item_uid'];
    qtde = json['qtde'];
    valor = json['valor'];
    valorTotal = json['valor_total'];
    descrProduto = json['descr_produto'];
    tamanho = json['tamanho'];
    textoOpc = json['texto_opc'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['item_uid'] = this.itemUid;
    data['qtde'] = this.qtde;
    data['valor'] = this.valor;
    data['valor_total'] = this.valorTotal;
    data['descr_produto'] = this.descrProduto;
    data['tamanho'] = this.tamanho;
    data['texto_opc'] = this.textoOpc;
    return data;
  }
}

Excerpt from API code via POST:

import 'package:cardapiomonitor/api/listar_pedido.dart';
import 'package:http/http.dart' as http;
import '../functions/fn_utils.dart';

class ListarPedidoApi {
  static Future<List<ListarPedido>> getListarPedido() async {
    var url = "http://192.168.1.12/api/ws_acoes.php";
    String lppeduuid = prefs.getString("PedUuid");

    var response = await http.post(
      url,
      headers: <String, String>{
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
      },
      body: <String, String>{
        'pedido': lppeduuid,
        'requisicao': 'LISTARPEDIDO',
      },
    );

    List listaResponseLP = json.decode(response.body);

    List<Itens> itens = List<Itens>();

    final listarpedidos = List<ListarPedido>();
    // final listarpedidositens = List<Itens>();
    // List<Itens> listarpedidositens = List<Itens>();

    for (Map mapLP in listaResponseLP) {
      ListarPedido lp = ListarPedido.fromJson(mapLP);
      listarpedidos.add(lp);

      Itens lpi = Itens.fromJson(mapLP);
      itens.add(lpi);
    }

    return listarpedidos;
  }
}

Excerpt from the code:

_body() {
    Future<List<ListarPedido>> listarpedidos =
        ListarPedidoApi.getListarPedido();
    return RefreshIndicator(
      onRefresh: _getDataListarPedidos,
      backgroundColor: Colors.black54,
      child: FutureBuilder(
        future: listarpedidos,
        builder: (context, snapshot) {
          if (snapshot.hasError) {
            return Center(
              child: Text(
                "Seu token está errado.",
                style: TextStyle(
                  fontFamily: 'Roboto',
                ),
              ),
            );
          }

          if (!snapshot.hasData) {
            return Center(
              child: CircularProgressIndicator(),
            );
          }

          List<ListarPedido> listarpedidos = snapshot.data;
          return _listView(listarpedidos);
        },
      ),
    );
  }

  _listView(listarpedidos) {
    return ListView.builder(
      itemCount: listarpedidos != null ? listarpedidos.length : 0,
      itemBuilder: (context, index) {
        ListarPedido lp = listarpedidos[index];

        return SingleChildScrollView(
          padding: EdgeInsets.fromLTRB(2, 20, 2, 15),
          child: Column(
            children: <Widget>[
              ListTile(
                leading: Icon(
                  Icons.add_shopping_cart_rounded,
                  color: Colors.black,
                ),
                title: Text(
                  "Número do Pedido: " + lp.numero.toString(),
                  style: TextStyle(
                    fontSize: 15,
                    fontFamily: 'Roboto',
                  ),
                ),
              ),
              ListTile(
                leading: Icon(
                  Icons.attach_money,
                  color: Colors.black,
                ),
                title: Text(
                  "Local de Entrega: " + lp.localEntrega,
                  style: TextStyle(
                    fontSize: 15,
                    fontFamily: 'Roboto',
                  ),
                ),
              ),
              ListTile(
                leading: Icon(
                  Icons.format_size,
                  color: Colors.black,
                ),
                title: Text(
                  "Data e Hora: " + lp.dataHora,
                  style: TextStyle(
                    fontSize: 15,
                    fontFamily: 'Roboto',
                  ),
                ),
              ),
              ListTile(
                leading: Icon(
                  Icons.format_size,
                  color: Colors.black,
                ),
                title: Text(
                  "Forma de Pagamento: " + lp.formaPagamento,
                  style: TextStyle(
                    fontSize: 15,
                    fontFamily: 'Roboto',
                  ),
                ),
              ),
              Divider(
                color: Colors.red[600],
                height: 20,
                thickness: 3,
                indent: 20,
                endIndent: 20,
              ),
              _itens(),
            ],
          ),
        );
      },
    );
  }

  _itens() {
    Future<List<ListarPedido>> listarpedidos =
        ListarPedidoApi.getListarPedido();
    return RefreshIndicator(
      onRefresh: _getDataListarPedidos,
      backgroundColor: Colors.black54,
      child: FutureBuilder(
        future: listarpedidos,
        builder: (context, snapshot) {
          if (snapshot.hasError) {
            return Center(
              child: Text(
                "Seu token está errado.",
                style: TextStyle(
                  fontFamily: 'Roboto',
                ),
              ),
            );
          }

          if (!snapshot.hasData) {
            return Center(
              child: CircularProgressIndicator(),
            );
          }

          List<ListarPedido> listarpedidos = snapshot.data;
          return _itensView(listarpedidos);
        },
      ),
    );
  }

  _itensView(listarpedidos) {
    return ListView.builder(
      shrinkWrap: true,
      itemCount: listarpedidos != null ? listarpedidos.length : 0,
      itemBuilder: (context, index) {
        Itens lpi = listarpedidos[index];

        return SingleChildScrollView(
          padding: EdgeInsets.fromLTRB(2, 20, 2, 15),
          child: Column(
            children: <Widget>[
              ListTile(
                leading: Icon(
                  Icons.location_on,
                  color: Colors.black,
                ),
                title: Text(
                  lpi.itemUid,
                  style: TextStyle(
                    fontSize: 0,
                    fontFamily: 'Roboto',
                  ),
                ),
              ),
              ListTile(
                leading: Icon(
                  Icons.date_range,
                  color: Colors.black,
                ),
                title: Text(
                  "Quantidade: " + lpi.qtde,
                  style: TextStyle(
                    fontSize: 15,
                    fontFamily: 'Roboto',
                  ),
                ),
              ),
              ListTile(
                leading: Icon(
                  Icons.credit_card,
                  color: Colors.black,
                ),
                title: Text(
                  "Valor: " + lpi.valor,
                  style: TextStyle(
                    fontSize: 15,
                    fontFamily: 'Roboto',
                  ),
                ),
              ),
              ListTile(
                leading: Icon(
                  Icons.location_on,
                  color: Colors.black,
                ),
                title: Text(
                  "Valor Total: " + lpi.valorTotal,
                  style: TextStyle(
                    fontSize: 15,
                    fontFamily: 'Roboto',
                  ),
                ),
              ),
              ListTile(
                leading: Icon(
                  Icons.location_on,
                  color: Colors.black,
                ),
                title: Text(
                  "Descrição Produto: " + lpi.descrProduto,
                  style: TextStyle(
                    fontSize: 15,
                    fontFamily: 'Roboto',
                  ),
                ),
              ),
              ListTile(
                leading: Icon(
                  Icons.location_on,
                  color: Colors.black,
                ),
                title: Text(
                  "Tamanho: " + lpi.tamanho,
                  style: TextStyle(
                    fontSize: 15,
                    fontFamily: 'Roboto',
                  ),
                ),
              ),
              ListTile(
                leading: Icon(
                  Icons.location_on,
                  color: Colors.black,
                ),
                title: Text(
                  "Opcionais: " + lpi.textoOpc,
                  style: TextStyle(
                    fontSize: 15,
                    fontFamily: 'Roboto',
                  ),
                ),
              ),
            ],
          ),
        );
      },
    );
  }

And I get that mistake:

type 'ListarPedido' is not a subtype of type 'Itens'

Why make that mistake???

  • This error occurs because you are trying to feed a variable of the type Itens with some other kind of value ListarPedido... The problem is possibly here Itens lpi = listarpedidos[index]; you need to change to Itens lpi = listarpedidos[index][0]; (Or another value other than 0.

  • I received this error: Class 'Listedido 'has no instance method '[]'. Receiver: Instance of 'Listedido' Tried Calling:

  • Ops was a lack of attention, a little glance... What I said was missing the name of the property, actually it is `Items lpi = listings[index]. items[0];

  • It worked out here, thank you very much, only that from the list of items only you bring a list, you would have some suggestion?

  • Hence it is you who have to solve, will according to the logic of how you intend to display the items... You would have to go through all the items in the list and go displaying each one on the screen.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.