Flutter / JSON: instance member cannot be accessed using static access

374

The error means version is not a Static variable. So to access it like this LatestVersion.version either make version a static variable or

Replace

Text(LatestVersion.version),

with

Text(LatestVersion().version),

If everything else is correct in your code the above should work.

Share:
374
beansbeans
Author by

beansbeans

Updated on December 26, 2022

Comments

  • beansbeans
    beansbeans over 1 year

    Here is a complete code snippet that should demonstrate the issue that I am bumping into.

    import 'package:flutter/material.dart';
    import 'dart:async';
    import 'dart:convert';
    import 'package:http/http.dart' as http;
    import 'package:flutter/rendering.dart';
    import 'package:flutter/services.dart';
    
    void main() async {
      WidgetsFlutterBinding.ensureInitialized();
      SystemChrome.setPreferredOrientations([
        DeviceOrientation.landscapeLeft,
        DeviceOrientation.landscapeRight,
      ]);
      SystemChrome.setEnabledSystemUIOverlays([]);
      runApp(
        MaterialApp(
          home: MyHomePage(),
        ),
      );
    }
    
    class MyHomePage extends StatefulWidget {
      MyHomePage({Key key,}) : super(key: key);
    
      @override
      _MyHomePageState createState() => _MyHomePageState();
    }
    
    Future<latestVersion> fetchAlbum() async {
      final response = await http.get('https://api.jsonbin.io/b/5fd25941bef8b7699e57dce9');
      if (response.statusCode == 200) {
        print('yay');
        return latestVersion.fromJson(jsonDecode(response.body));
      } else {
        print('nay');
        throw Exception('Failed to load version');
      }
    }
    
    class latestVersion {
      final String title;
      final String version;
    
      latestVersion({this.version, this.title});
    
      factory latestVersion.fromJson(Map<String, dynamic> json) {
        return latestVersion(version: json['version'], title: json['title'],
        );
      }
    }
    
    class _MyHomePageState extends State<MyHomePage> {
      static Future<latestVersion> futureAlbum;
    
      @override
      void initState() {
        super.initState();
        futureAlbum = fetchAlbum();
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          backgroundColor: Colors.yellow,
          body: Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Text(''),
                Text('CURRENT'),
                Text('---------'),
                Text('0.37'),
                Text(''),
                Text('LATEST'),
                Text('--------'),
                Text(latestVersion.version),
                Text(''),
                Text(''),
              ],
            ),
          ),
        );
      }
    }
    

    When trying to run this code, I get an error at line 76.

    "instance member 'version' cannot be accessed using static access"
    

    How exactly can I access this json-decoded variable? Thank you in advance. I'm new to working with asynchronous functions and Future and would appreciate any help that can be given.

  • beansbeans
    beansbeans over 3 years
    Thank you. This is absolutely correct. Now I'm bumping into an issue that my value is null. but that's a different problem that I'll need to handle on my own. Thank you so much for your help.