Home > other >  Getting substring of a string in flutter that contain and emoji can cause a render failure
Getting substring of a string in flutter that contain and emoji can cause a render failure

Time:01-30

As part of the app I am shortening a user-given string to 40 characters and appending ellipses at the end if the string is longer than 40 characters. The users are allowed/able to use emoji in their strings.

If a string that is cut off has an emoji at the 40-character mark it causes it to fail to render and renders as a "�".

Is there a way to reliably get a sub-string of a text but without running into this issue?

Current code:

if (useStr.length > 40) {
useStr = useStr.substring(0, 40)   "...";
}

CodePudding user response:

You can use the "flutter_string_encoding" library, which provides string width calculation in terms of display width, taking into account full-width characters like emoji.

import 'package:flutter_string_encoding/flutter_string_encoding.dart';

String useStr = "user-given string with emoji            
  • Related