Skip to content

fix: Imports definitely need to be improved. #404

@felipecastrosales

Description

@felipecastrosales

Description
Hey, your recent update with stac_core was interesting, but it introduced several import issues in my project. I’d like some help understanding how I can resolve them globally, by importing only stac_core.dart and/or stac.dart.

I noticed that now I have to import many different files instead of just relying on the barrel file.

For example, in this code:

import 'dart:io';

import 'package:flutter/material.dart';

class Test extends StatelessWidget {
  const Test({super.key, required this.model});

  final Map<String, dynamic> model;

  @override
  Widget build(BuildContext context) {
    Image? buildImageFromJson(Map<String, dynamic>? imageJson) {
      if (imageJson == null) {
        return null;
      }

      final src = imageJson['src'] as String?;
      if (src == null || src.isEmpty) {
        return null;
      }

      final width = (imageJson['width'] as num?)?.toDouble();
      final height = (imageJson['height'] as num?)?.toDouble();
      final fit = StacImage.fromJson(imageJson).fit;

      if (src.contains('.svg')) {
        debugPrint(
          '[test] SVG images are not supported for scratch cover',
        );
        return null;
      }

      return switch (src) {
        _ when src.startsWith('http') || src.startsWith('https') =>
          Image.network(
            src,
            width: width,
            height: height,
            fit: fit,
            errorBuilder: (context, error, stackTrace) => const SizedBox(),
          ),
        _ when src.startsWith('file') => Image.file(
          File(src),
          width: width,
          height: height,
          fit: fit,
          errorBuilder: (context, error, stackTrace) => const SizedBox(),
        ),
        _ when src.startsWith('assets') => Image.asset(
          src,
          width: width,
          height: height,
          fit: fit.parse,
          errorBuilder: (context, error, stackTrace) => const SizedBox(),
        ),
        _ => null,
      };
    }

    final imageForCover = buildImageFromJson(model['imageForCover']);

    return Scaffold(
      appBar: AppBar(title: const Text('')),
      body: Stack(
        children: [
          if (imageForCover != null)
            Container(
              decoration: BoxDecoration(
                image: DecorationImage(image: imageForCover.image),
              ),
            ),

          Stac.fromJson(model, context) ?? const SizedBox(),
        ],
      ),
    );
  }
}

This generates the following error: Image

Expected Behavior
How can I make it work using only the stac.dart and/or stac_core.dart import? I wouldn’t like to import multiple files, especially since there is a barrel file available.

In many cases, migrating to the new format becomes difficult. There are several other examples I could mention, but it seems that the migration to the new format made things harder and significantly changed the way imports and constructions work, especially for projects that need to create many parsers due to their complexity.

How could we simplify this?

It also seems at least a bit strange to me that an implementation of StacBoxFit exists in two different places—one in the core and another in the official repository. Why wasn’t a simpler approach taken? That way, a single file could be imported and everything would work correctly, without the issues that appeared after the migration.

Image

I’d also like to thank the package support team and say that the work being done on it is amazing—one of the best in the Flutter ecosystem right now. However, this specific point, in my opinion, is something that should be improved or revisited.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions