Can't get variable in another class – Flutter

Issue

Tell me, I need to get a hash variable in a cubit file which is in block_info_page. I need this variable to make an API request to block_repository that uses the hash_block variable. Is it possible to implement this? Thanks in advance.

cubit

class BlockinfoCubit extends Cubit<BlockInfoState> {
  final BlockInfoRepository _blockInfoRepository;

  BlockinfoCubit(this._blockInfoRepository) : super(BlockInfoInitialState());

  void getBlockInfo() async {
    emit(BlockInfoLoadingState());
    try {
      final hash_block = ; 
      final blocksInfo = await _blockInfoRepository.getBlockInfo(hash_block);
      emit(BlockInfoLoadedState(blocksInfo));
    } catch (e) {
      emit(BlockInfoErrorState(e.toString()));
    }
  }
}

block_info_page

class BlockInfoPage extends StatelessWidget {
  final String hash;
  const BlockInfoPage({Key? key, required this.hash}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.white,
        title: Image.asset(
          'assets/images/logo.png',
          height: 35,
        ),
        centerTitle: false,
      ),
      body: Center(child: Text(hash)),
    );
  }
}

block_repository

class BlockInfoRepository {
  Future<List<BlockInfoModel>> getBlockInfo(hash_block) async {
    final response = await http
        .get(Uri.parse('https://blockchain.../$hash_block'));

    if (response.hashCode == 200) {
      final blockInfo = json.decode(response.body);

      return blockInfo
          .map<BlockInfoModel>((json) => BlockInfoModel.fromJson(json))
          .toList();
    } else {
      throw Exception('Failed to load block-info');
    }
  }
}

block_page

TextButton(
                              onPressed: () => Navigator.push(
                                  context,
                                  MaterialPageRoute(
                                      builder: (context) => BlockInfoPage(
                                          hash: blocksData[index].hash))),

Solution

that’s really easy.

first import the block_info_page that has the variable inside.

then you have 2 ways:

  1. make the hash variable static: static String hash;
  2. for the other way you don’t need to do now anything

WAY 1

you can get the variable like this:
BlockInfoPage.hash;

what it does is: pick the variable out of the class without build the whole class again


WAY 2

you can get the variable like this:
BlockInfoPage().hash;

what it does is: build the whole class again and after this it pick the variable out of the class


if that don’t work make the variable global – put the hash variable outside of your class

you can access to the global variable like this:

  1. first import the file that has the global variable inside
  2. you can acces to your global variable like you was in the file that has the global variable inside: hash

PS: because your global variable is not more inside of the class it never will set automatically to the begining value

Answered By – liam spiegel

Answer Checked By – Candace Johnson (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published.