【问题标题】:Flutter type 'Null' is not a subtype of type 'RenderBox' in type castFlutter 类型“Null”不是类型转换中“RenderBox”类型的子类型
【发布时间】:2021-11-28 07:15:52
【问题描述】:

在构建 MediaQuery(MediaQueryData(size: Size(485.4, 814.8), devicePixelRatio: 2.2, textScaleFactor: 1.0, platformBrightness: Brightness.dark, padding: E​​dgeInsets.zero, viewPadding: E​​dgeInsets.zero, viewInsets: EdgeInsets.zero,alwaysUse24HourFormat:false,accessibleNavigation:false,highContrast:false,disableAnimations:false,invertColors:false,boldText:false,navigationMode:传统)): “Null”类型不是类型转换中“RenderBox”类型的子类型

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class Small extends StatefulWidget {
  Small({Key? key}) : super(key: key);

  @override
  _SmallState createState() => _SmallState();
}

class _SmallState extends State<Small> {
  late Keys keys;
  late List<Offset> positions;
  /*@override
  void initState() {
    super.initState();
    keys = new Keys();
    print(positions.length);
  }*/

  _SmallState() {
    this.keys = new Keys();
    this.positions = [];
    this.initOffsetsPositions();
  }

  List<String> images = [
    'https://static.javatpoint.com/tutorial/flutter/images/flutter-logo.png',
    'https://simg.nicepng.com/png/small/107-1076994_white-square-outline-plain-black-background-square.png',
    'https://static.javatpoint.com/tutorial/flutter/images/flutter-logo.png',
    'https://static.javatpoint.com/tutorial/flutter/images/flutter-logo.png',
    'https://static.javatpoint.com/tutorial/flutter/images/flutter-logo.png',
    'https://static.javatpoint.com/tutorial/flutter/images/flutter-logo.png',
    'https://static.javatpoint.com/tutorial/flutter/images/flutter-logo.png',
    'https://static.javatpoint.com/tutorial/flutter/images/flutter-logo.png',
    'https://static.javatpoint.com/tutorial/flutter/images/flutter-logo.png'
  ];
  //Init arrays of positions
  void initOffsetsPositions() {
    late List<GlobalKey> keyList = keys.getListOfKeys();
    print(keyList);
    for (int i = 0; i < keyList.length; i++) {
      this.positions[i] = this.getOffsetsPositions(keyList[i]);
      print(positions[i]);
    }
  }

  Offset getOffsetsPositions(GlobalKey key) {
    late RenderBox box = key.currentContext?.findRenderObject() as RenderBox;
    late Offset position = box.globalToLocal(Offset.zero);
    late Size size = box.size; // or _widgetKey.currentContext?.size
    print('Size: ${size.width}, ${size.height}');
    return position;
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Container(
        padding: EdgeInsets.all(12.0),
        child: GridView.builder(
          itemCount: images.length,
          gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
              crossAxisCount: 3, crossAxisSpacing: 4.0, mainAxisSpacing: 4.0),
          itemBuilder: (BuildContext context, int index) {
            return Stack(
              children: [
                Positioned(
                  child: Container(
                    key: keys.getKeyOfList(index),
                    child: Image.network(images[index]),
                  ),
                ),
              ],
            );
          },
        ),
      ),
    );
  }
}

class Keys {
  static final key0 = GlobalKey();
  static final key1 = GlobalKey();
  static final key2 = GlobalKey();
  static final key3 = GlobalKey();
  static final key4 = GlobalKey();
  static final key5 = GlobalKey();
  static final key6 = GlobalKey();
  static final key7 = GlobalKey();
  static final key8 = GlobalKey();
  List<GlobalKey> keyList = [
    key0,
    key1,
    key2,
    key3,
    key4,
    key5,
    key6,
    key7,
    key8,
  ];
  GlobalKey getKeyOfList(index) {
    return keyList[index];
  }

  List<GlobalKey> getListOfKeys() {
    return keyList;
  }
}

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    解决方案。 处于初始化状态

      @override
      void initState() {
        super.initState();
        //keys = new Keys();
        WidgetsBinding.instance!.addPostFrameCallback((_) {
          this.initOffsetsPositions();
        });
      }
    

    【讨论】:

      猜你喜欢
      • 2023-04-07
      • 1970-01-01
      • 2022-07-06
      • 1970-01-01
      • 2021-10-22
      • 2023-03-03
      • 1970-01-01
      • 2022-09-24
      相关资源
      最近更新 更多