Home > database >  Unhandled Exception: NoSuchMethodError: The method '[]' was called on null. Where is the v
Unhandled Exception: NoSuchMethodError: The method '[]' was called on null. Where is the v

Time:01-27

I get this error in my code where I get stocks data from an API and the link changes according to a for loop on a list in the same class. my code is below.

ive tried calling the code like this

        value: jsonDecode(mydata.body)["Time Series (5min)"][0]["1. open"],
        change: jsonDecode(mydata.body)["Time Series (5min)"][0]["5. volume"],

it dosent work. please help this is the main code.

import 'package:flutter/material.dart';
import '../models/stock_details.dart';
import 'package:http/http.dart';
import 'dart:convert';

class StockList extends StatefulWidget {
  @override
  State<StockList> createState() => _StockListState();
}

class _StockListState extends State<StockList> {
  @override
  void initState() {
    getData();
    super.initState();
  }

  void getData() async {
    int i = 1;
    List<String> initalstocks = ["TCS", "BTC", "IBM"];
    for (var element in initalstocks) {
      Response mydata = await get(Uri.parse(
          "https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=$element&interval=5min&apikey=RTILMV6RV0R20DKC"));
      Map decodeBodyData = jsonDecode(mydata.body);
      Map body_data = decodeBodyData["Time Series (5min)"];
      String open = body_data[0]["1. open".toString()];
      print(open);
      stockname.add(Stock(
        id: i.toString(),
        name: element,
        value: 1,
        change: 2,
      ));
      i  = 1;
    }
  }

  final List<Stock> stockname = [];

  @override
  Widget build(BuildContext context) {
    return ListView(
      children: [
        const SizedBox(
          height: 1,
          width: double.infinity,
          child: DecoratedBox(
            decoration: BoxDecoration(color: Colors.grey),
          ),
        ),
        Column(
          children: stockname.map((stx) {
            return Column(
              children: [
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Container(
                      height: 100,
                      alignment: Alignment.centerLeft,
                      child: Text(
                        stx.name,
                        style: const TextStyle(
                          color: Colors.white,
                        ),
                      ),
                    ),
                    Column(
                      children: [
                        Text(
                          '\$${stx.value.toString()}',
                          style: const TextStyle(
                            color: Colors.white,
                          ),
                        ),
                        Text(
                          '\$${stx.change.toString()}',
                          style: const TextStyle(
                            color: Colors.white,
                          ),
                        ),
                      ],
                    )
                  ],
                ),
                const SizedBox(
                  height: 1,
                  width: double.infinity,
                  child: DecoratedBox(
                    decoration: BoxDecoration(color: Colors.grey),
                  ),
                ),
              ],
            );
          }).toList(),
        ),
      ],
    );
  }
}

Tis is class constructor i use to map data into the widgets.


import 'package:flutter/material.dart';

class Stock {
  var id;
  var name;
  var value;
  var change;
  Stock({
    this.id,
    this.name,
    this.value,
    this.change,
  });
}

CodePudding user response:

Trying out your url like https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=TCS&interval=5min&apikey=RTILMV6RV0R20DKC gives:

{
    "Meta Data": {
        "1. Information": "Intraday (5min) open, high, low, close prices and volume",
        "2. Symbol": "TCS",
        "3. Last Refreshed": "2023-01-26 19:05:00",
        "4. Interval": "5min",
        "5. Output Size": "Compact",
        "6. Time Zone": "US/Eastern"
    },
    "Time Series (5min)": {
        "2023-01-26 19:05:00": {
            "1. open": "5.1200",
            "2. high": "5.1200",
            "3. low": "5.1200",
            "4. close": "5.1200",
            "5. volume": "500"
        },
        "2023-01-26 16:00:00": {
            "1. open": "5.0900",
            "2. high": "5.1100",
            "3. low": "5.0800",
            "4. close": "5.1100",
            "5. volume": "11474"
        },
        "2023-01-26 15:55:00": {
            "1. open": "5.0700",
            "2. high": "5.1100",
            "3. low": "5.0700",
            "4. close": "5.0900",
            "5. volume": "8234"
        },
        "2023-01-26 15:50:00": {
            "1. open": "5.0800",
            "2. high": "5.0800",
            "3. low": "5.0700",
            "4. close": "5.0700",
            "5. volume": "3264"
        },
        "2023-01-26 15:45:00": {
            "1. open": "5.0850",
            "2. high": "5.0850",
            "3. low": "5.0800",
            "4. close": "5.0800",
            "5. volume": "1304"
        },
        "2023-01-26 15:40:00": {
            "1. open": "5.1050",
            "2. high": "5.1050",
            "3. low": "5.0800",
            "4. close": "5.0800",
            "5. volume": "5462"
        },
        "2023-01-26 15:35:00": {
            "1. open": "5.1050",
            "2. high": "5.1100",
            "3. low": "5.1000",
            "4. close": "5.1100",
            "5. volume": "918"
        },
        "2023-01-26 15:30:00": {
            "1. open": "5.0950",
            "2. high": "5.1100",
            "3. low": "5.0950",
            "4. close": "5.1100",
            "5. volume": "7920"
        },
        "2023-01-26 15:25:00": {
            "1. open": "5.1150",
            "2. high": "5.1150",
            "3. low": "5.0850",
            "4. close": "5.0950",
            "5. volume": "3245"
        },
        "2023-01-26 15:20:00": {
            "1. open": "5.1100",
            "2. high": "5.1150",
            "3. low": "5.1000",
            "4. close": "5.1100",
            "5. volume": "2084"
        },
        "2023-01-26 15:15:00": {
            "1. open": "5.1159",
            "2. high": "5.1159",
            "3. low": "5.1000",
            "4. close": "5.1000",
            "5. volume": "1153"
        },
        "2023-01-26 15:10:00": {
            "1. open": "5.1300",
            "2. high": "5.1300",
            "3. low": "5.1100",
            "4. close": "5.1100",
            "5. volume": "1867"
        },
        "2023-01-26 15:05:00": {
            "1. open": "5.1200",
            "2. high": "5.1264",
            "3. low": "5.1200",
            "4. close": "5.1264",
            "5. volume": "302"
        },
        "2023-01-26 15:00:00": {
            "1. open": "5.1550",
            "2. high": "5.1550",
            "3. low": "5.1300",
            "4. close": "5.1300",
            "5. volume": "4353"
        },
        "2023-01-26 14:55:00": {
            "1. open": "5.1600",
            "2. high": "5.1600",
            "3. low": "5.1550",
            "4. close": "5.1558",
            "5. volume": "621"
        },
        "2023-01-26 14:50:00": {
            "1. open": "5.1600",
            "2. high": "5.1612",
            "3. low": "5.1500",
            "4. close": "5.1500",
            "5. volume": "532"
        },
        "2023-01-26 14:45:00": {
            "1. open": "5.1600",
            "2. high": "5.1700",
            "3. low": "5.1500",
            "4. close": "5.1600",
            "5. volume": "3595"
        },
        "2023-01-26 14:30:00": {
            "1. open": "5.1600",
            "2. high": "5.1600",
            "3. low": "5.1600",
            "4. close": "5.1600",
            "5. volume": "101"
        },
        "2023-01-26 14:25:00": {
            "1. open": "5.1600",
            "2. high": "5.1700",
            "3. low": "5.1600",
            "4. close": "5.1600",
            "5. volume": "2419"
        },
        "2023-01-26 14:20:00": {
            "1. open": "5.1600",
            "2. high": "5.1600",
            "3. low": "5.1600",
            "4. close": "5.1600",
            "5. volume": "708"
        },
        "2023-01-26 14:15:00": {
            "1. open": "5.1600",
            "2. high": "5.1600",
            "3. low": "5.1600",
            "4. close": "5.1600",
            "5. volume": "440"
        },
        "2023-01-26 14:05:00": {
            "1. open": "5.1600",
            "2. high": "5.1600",
            "3. low": "5.1600",
            "4. close": "5.1600",
            "5. volume": "901"
        },
        "2023-01-26 14:00:00": {
            "1. open": "5.1611",
            "2. high": "5.1611",
            "3. low": "5.1600",
            "4. close": "5.1600",
            "5. volume": "2275"
        },
        "2023-01-26 13:55:00": {
            "1. open": "5.1650",
            "2. high": "5.1650",
            "3. low": "5.1650",
            "4. close": "5.1650",
            "5. volume": "100"
        },
        "2023-01-26 13:50:00": {
            "1. open": "5.1600",
            "2. high": "5.1650",
            "3. low": "5.1600",
            "4. close": "5.1650",
            "5. volume": "304"
        },
        "2023-01-26 13:45:00": {
            "1. open": "5.1608",
            "2. high": "5.1650",
            "3. low": "5.1500",
            "4. close": "5.1600",
            "5. volume": "2632"
        },
        "2023-01-26 13:40:00": {
            "1. open": "5.1600",
            "2. high": "5.1643",
            "3. low": "5.1600",
            "4. close": "5.1643",
            "5. volume": "918"
        },
        "2023-01-26 13:35:00": {
            "1. open": "5.1500",
            "2. high": "5.1600",
            "3. low": "5.1500",
            "4. close": "5.1600",
            "5. volume": "1345"
        },
        "2023-01-26 13:30:00": {
            "1. open": "5.1500",
            "2. high": "5.1500",
            "3. low": "5.1400",
            "4. close": "5.1450",
            "5. volume": "514"
        },
        "2023-01-26 13:25:00": {
            "1. open": "5.1500",
            "2. high": "5.1500",
            "3. low": "5.1400",
            "4. close": "5.1400",
            "5. volume": "406"
        },
        "2023-01-26 13:20:00": {
            "1. open": "5.1500",
            "2. high": "5.1500",
            "3. low": "5.1500",
            "4. close": "5.1500",
            "5. volume": "250"
        },
        "2023-01-26 13:10:00": {
            "1. open": "5.1600",
            "2. high": "5.1600",
            "3. low": "5.1500",
            "4. close": "5.1500",
            "5. volume": "1220"
        },
        "2023-01-26 13:05:00": {
            "1. open": "5.1600",
            "2. high": "5.1600",
            "3. low": "5.1500",
            "4. close": "5.1500",
            "5. volume": "3029"
        },
        "2023-01-26 13:00:00": {
            "1. open": "5.1600",
            "2. high": "5.1600",
            "3. low": "5.1600",
            "4. close": "5.1600",
            "5. volume": "100"
        },
        "2023-01-26 12:55:00": {
            "1. open": "5.1400",
            "2. high": "5.1600",
            "3. low": "5.1400",
            "4. close": "5.1600",
            "5. volume": "1380"
        },
        "2023-01-26 12:50:00": {
            "1. open": "5.1275",
            "2. high": "5.1275",
            "3. low": "5.1275",
            "4. close": "5.1275",
            "5. volume": "811"
        },
        "2023-01-26 12:45:00": {
            "1. open": "5.1272",
            "2. high": "5.1400",
            "3. low": "5.1200",
            "4. close": "5.1300",
            "5. volume": "2338"
        },
        "2023-01-26 12:40:00": {
            "1. open": "5.1300",
            "2. high": "5.1300",
            "3. low": "5.1300",
            "4. close": "5.1300",
            "5. volume": "100"
        },
        "2023-01-26 12:35:00": {
            "1. open": "5.1300",
            "2. high": "5.1300",
            "3. low": "5.1300",
            "4. close": "5.1300",
            "5. volume": "200"
        },
        "2023-01-26 12:30:00": {
            "1. open": "5.1400",
            "2. high": "5.1400",
            "3. low": "5.1300",
            "4. close": "5.1300",
            "5. volume": "210"
        },
        "2023-01-26 12:15:00": {
            "1. open": "5.1300",
            "2. high": "5.1300",
            "3. low": "5.1300",
            "4. close": "5.1300",
            "5. volume": "677"
        },
        "2023-01-26 12:10:00": {
            "1. open": "5.1300",
            "2. high": "5.1300",
            "3. low": "5.1300",
            "4. close": "5.1300",
            "5. volume": "957"
        },
        "2023-01-26 12:05:00": {
            "1. open": "5.1100",
            "2. high": "5.1200",
            "3. low": "5.1100",
            "4. close": "5.1115",
            "5. volume": "1349"
        },
        "2023-01-26 12:00:00": {
            "1. open": "5.1100",
            "2. high": "5.1100",
            "3. low": "5.1100",
            "4. close": "5.1100",
            "5. volume": "294"
        },
        "2023-01-26 11:55:00": {
            "1. open": "5.1100",
            "2. high": "5.1200",
            "3. low": "5.1100",
            "4. close": "5.1200",
            "5. volume": "1289"
        },
        "2023-01-26 11:45:00": {
            "1. open": "5.1100",
            "2. high": "5.1100",
            "3. low": "5.1100",
            "4. close": "5.1100",
            "5. volume": "340"
        },
        "2023-01-26 11:40:00": {
            "1. open": "5.1100",
            "2. high": "5.1300",
            "3. low": "5.1100",
            "4. close": "5.1100",
            "5. volume": "7564"
        },
        "2023-01-26 11:35:00": {
            "1. open": "5.1000",
            "2. high": "5.1100",
            "3. low": "5.1000",
            "4. close": "5.1100",
            "5. volume": "796"
        },
        "2023-01-26 11:25:00": {
            "1. open": "5.0900",
            "2. high": "5.0900",
            "3. low": "5.0900",
            "4. close": "5.0900",
            "5. volume": "300"
        },
        "2023-01-26 11:20:00": {
            "1. open": "5.0900",
            "2. high": "5.1000",
            "3. low": "5.0900",
            "4. close": "5.1000",
            "5. volume": "500"
        },
        "2023-01-26 11:10:00": {
            "1. open": "5.0900",
            "2. high": "5.0900",
            "3. low": "5.0800",
            "4. close": "5.0800",
            "5. volume": "300"
        },
        "2023-01-26 11:05:00": {
            "1. open": "5.0800",
            "2. high": "5.0800",
            "3. low": "5.0715",
            "4. close": "5.0800",
            "5. volume": "1236"
        },
        "2023-01-26 11:00:00": {
            "1. open": "5.0601",
            "2. high": "5.0800",
            "3. low": "5.0601",
            "4. close": "5.0700",
            "5. volume": "2800"
        },
        "2023-01-26 10:55:00": {
            "1. open": "5.0700",
            "2. high": "5.0700",
            "3. low": "5.0700",
            "4. close": "5.0700",
            "5. volume": "400"
        },
        "2023-01-26 10:50:00": {
            "1. open": "5.0209",
            "2. high": "5.0650",
            "3. low": "5.0209",
            "4. close": "5.0650",
            "5. volume": "8281"
        },
        "2023-01-26 10:45:00": {
            "1. open": "5.0222",
            "2. high": "5.0222",
            "3. low": "5.0200",
            "4. close": "5.0200",
            "5. volume": "853"
        },
        "2023-01-26 10:40:00": {
            "1. open": "5.0400",
            "2. high": "5.0400",
            "3. low": "5.0200",
            "4. close": "5.0200",
            "5. volume": "692"
        },
        "2023-01-26 10:35:00": {
            "1. open": "5.0400",
            "2. high": "5.0400",
            "3. low": "5.0400",
            "4. close": "5.0400",
            "5. volume": "102"
        },
        "2023-01-26 10:30:00": {
            "1. open": "5.0500",
            "2. high": "5.0500",
            "3. low": "5.0500",
            "4. close": "5.0500",
            "5. volume": "1703"
        },
        "2023-01-26 10:20:00": {
            "1. open": "5.0500",
            "2. high": "5.0500",
            "3. low": "5.0490",
            "4. close": "5.0490",
            "5. volume": "716"
        },
        "2023-01-26 10:15:00": {
            "1. open": "5.0500",
            "2. high": "5.0500",
            "3. low": "5.0400",
            "4. close": "5.0400",
            "5. volume": "1959"
        },
        "2023-01-26 10:10:00": {
            "1. open": "5.0400",
            "2. high": "5.0450",
            "3. low": "5.0350",
            "4. close": "5.0350",
            "5. volume": "5542"
        },
        "2023-01-26 10:05:00": {
            "1. open": "5.0400",
            "2. high": "5.0450",
            "3. low": "5.0400",
            "4. close": "5.0450",
            "5. volume": "1231"
        },
        "2023-01-26 10:00:00": {
            "1. open": "5.0500",
            "2. high": "5.0500",
            "3. low": "5.0300",
            "4. close": "5.0500",
            "5. volume": "6856"
        },
        "2023-01-26 09:40:00": {
            "1. open": "5.0736",
            "2. high": "5.0800",
            "3. low": "5.0736",
            "4. close": "5.0800",
            "5. volume": "1222"
        },
        "2023-01-26 09:35:00": {
            "1. open": "5.0500",
            "2. high": "5.1000",
            "3. low": "5.0500",
            "4. close": "5.1000",
            "5. volume": "2534"
        },
        "2023-01-25 16:05:00": {
            "1. open": "5.0100",
            "2. high": "5.0100",
            "3. low": "5.0100",
            "4. close": "5.0100",
            "5. volume": "755"
        },
        "2023-01-25 16:00:00": {
            "1. open": "5.0000",
            "2. high": "5.0200",
            "3. low": "5.0000",
            "4. close": "5.0100",
            "5. volume": "11066"
        },
        "2023-01-25 15:55:00": {
            "1. open": "5.0150",
            "2. high": "5.0150",
            "3. low": "5.0000",
            "4. close": "5.0050",
            "5. volume": "4068"
        },
        "2023-01-25 15:50:00": {
            "1. open": "5.0100",
            "2. high": "5.0200",
            "3. low": "5.0100",
            "4. close": "5.0150",
            "5. volume": "2870"
        },
        "2023-01-25 15:45:00": {
            "1. open": "5.0150",
            "2. high": "5.0150",
            "3. low": "5.0100",
            "4. close": "5.0150",
            "5. volume": "2122"
        },
        "2023-01-25 15:40:00": {
            "1. open": "5.0150",
            "2. high": "5.0150",
            "3. low": "5.0150",
            "4. close": "5.0150",
            "5. volume": "1753"
        },
        "2023-01-25 15:35:00": {
            "1. open": "5.0200",
            "2. high": "5.0250",
            "3. low": "5.0100",
            "4. close": "5.0150",
            "5. volume": "6132"
        },
        "2023-01-25 15:30:00": {
            "1. open": "5.0200",
            "2. high": "5.0300",
            "3. low": "5.0200",
            "4. close": "5.0300",
            "5. volume": "1332"
        },
        "2023-01-25 15:25:00": {
            "1. open": "5.0200",
            "2. high": "5.0300",
            "3. low": "5.0101",
            "4. close": "5.0200",
            "5. volume": "2641"
        },
        "2023-01-25 15:20:00": {
            "1. open": "5.0050",
            "2. high": "5.0200",
            "3. low": "5.0050",
            "4. close": "5.0200",
            "5. volume": "2011"
        },
        "2023-01-25 15:15:00": {
            "1. open": "4.9900",
            "2. high": "5.0300",
            "3. low": "4.9900",
            "4. close": "5.0000",
            "5. volume": "17228"
        },
        "2023-01-25 15:10:00": {
            "1. open": "4.9900",
            "2. high": "4.9900",
            "3. low": "4.9900",
            "4. close": "4.9900",
            "5. volume": "2124"
        },
        "2023-01-25 15:05:00": {
            "1. open": "4.9900",
            "2. high": "4.9900",
            "3. low": "4.9900",
            "4. close": "4.9900",
            "5. volume": "930"
        },
        "2023-01-25 15:00:00": {
            "1. open": "4.9750",
            "2. high": "5.0000",
            "3. low": "4.9750",
            "4. close": "4.9900",
            "5. volume": "3231"
        },
        "2023-01-25 14:55:00": {
            "1. open": "4.9750",
            "2. high": "4.9750",
            "3. low": "4.9750",
            "4. close": "4.9750",
            "5. volume": "100"
        },
        "2023-01-25 14:50:00": {
            "1. open": "4.9700",
            "2. high": "4.9800",
            "3. low": "4.9700",
            "4. close": "4.9800",
            "5. volume": "532"
        },
        "2023-01-25 14:45:00": {
            "1. open": "4.9700",
            "2. high": "4.9700",
            "3. low": "4.9600",
            "4. close": "4.9600",
            "5. volume": "242"
        },
        "2023-01-25 14:40:00": {
            "1. open": "4.9700",
            "2. high": "4.9700",
            "3. low": "4.9600",
            "4. close": "4.9600",
            "5. volume": "211"
        },
        "2023-01-25 14:35:00": {
            "1. open": "4.9550",
            "2. high": "4.9700",
            "3. low": "4.9550",
            "4. close": "4.9700",
            "5. volume": "1513"
        },
        "2023-01-25 14:30:00": {
            "1. open": "4.9700",
            "2. high": "4.9700",
            "3. low": "4.9600",
            "4. close": "4.9600",
            "5. volume": "1736"
        },
        "2023-01-25 14:25:00": {
            "1. open": "4.9700",
            "2. high": "4.9800",
            "3. low": "4.9700",
            "4. close": "4.9700",
            "5. volume": "1467"
        },
        "2023-01-25 14:20:00": {
            "1. open": "4.9700",
            "2. high": "4.9800",
            "3. low": "4.9700",
            "4. close": "4.9800",
            "5. volume": "3459"
        },
        "2023-01-25 14:15:00": {
            "1. open": "4.9400",
            "2. high": "4.9700",
            "3. low": "4.9400",
            "4. close": "4.9700",
            "5. volume": "4816"
        },
        "2023-01-25 14:10:00": {
            "1. open": "4.9400",
            "2. high": "4.9413",
            "3. low": "4.9400",
            "4. close": "4.9413",
            "5. volume": "403"
        },
        "2023-01-25 14:05:00": {
            "1. open": "4.9300",
            "2. high": "4.9400",
            "3. low": "4.9300",
            "4. close": "4.9400",
            "5. volume": "586"
        },
        "2023-01-25 13:55:00": {
            "1. open": "4.9400",
            "2. high": "4.9400",
            "3. low": "4.9400",
            "4. close": "4.9400",
            "5. volume": "200"
        },
        "2023-01-25 13:50:00": {
            "1. open": "4.9400",
            "2. high": "4.9400",
            "3. low": "4.9400",
            "4. close": "4.9400",
            "5. volume": "853"
        },
        "2023-01-25 13:45:00": {
            "1. open": "4.9400",
            "2. high": "4.9400",
            "3. low": "4.9400",
            "4. close": "4.9400",
            "5. volume": "101"
        },
        "2023-01-25 13:40:00": {
            "1. open": "4.9400",
            "2. high": "4.9400",
            "3. low": "4.9400",
            "4. close": "4.9400",
            "5. volume": "126"
        },
        "2023-01-25 13:35:00": {
            "1. open": "4.9400",
            "2. high": "4.9400",
            "3. low": "4.9400",
            "4. close": "4.9400",
            "5. volume": "206"
        },
        "2023-01-25 13:30:00": {
            "1. open": "4.9400",
            "2. high": "4.9500",
            "3. low": "4.9400",
            "4. close": "4.9500",
            "5. volume": "301"
        },
        "2023-01-25 13:25:00": {
            "1. open": "4.9500",
            "2. high": "4.9500",
            "3. low": "4.9500",
            "4. close": "4.9500",
            "5. volume": "461"
        },
        "2023-01-25 13:20:00": {
            "1. open": "4.9400",
            "2. high": "4.9400",
            "3. low": "4.9300",
            "4. close": "4.9400",
            "5. volume": "661"
        },
        "2023-01-25 13:15:00": {
            "1. open": "4.9328",
            "2. high": "4.9328",
            "3. low": "4.9328",
            "4. close": "4.9328",
            "5. volume": "100"
        }
    }
}

The thing is, the first data point you try to get is not on:

jsonDecode(mydata.body)["Time Series (5min)"][0]["1. open"],

but on

jsonDecode(mydata.body)["Time Series (5min)"]["2023-01-26 19:05:00"]["1. open"],

"Time Series (5min)" is not a list but a map.

Because the keys seem to be dynamic dates it's harder to hardcode it, but this might work:

jsonDecode(mydata.body)["Time Series (5min)"].values.toList()[0]["1. open"],

CodePudding user response:

Problem is with your fetch is not successful as it is returing null,

Response mydata = await get(Uri.parse(
          "https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=$element&interval=5min&apikey=RTILMV6RV0R20DKC"));
      Map decodeBodyData = jsonDecode(mydata.body); //           
  • Related